Welcome to Lesson 3! In this lesson, we'll explore Python's dynamic typing system and cover common data types like numbers, strings, lists, tuples, and dictionaries.
One of the key differences between Python and C# is Python's dynamic typing. In C#, you need to explicitly declare the data type of a variable before using it. In Python, however, you don't need to specify the data type explicitly. The data type is determined dynamically at runtime based on the value assigned to the variable.
C# Code:
// C# - Explicitly declaring data type
int number = 42;
string name = "John";Python Equivalent:
# Python - Dynamic typing
number = 42
name = "John"-
Numbers:
- Integers (int): Whole numbers without a fractional part, e.g., 42, -10.
- Floating-Point Numbers (float): Numbers with a decimal point or in scientific notation, e.g., 3.14, -2.5e2.
Python Code:
# Numbers age = 30 pi = 3.14
-
Strings:
- Strings (str): Sequence of characters enclosed in single (' ') or double (" ") quotes.
Python Code:
# Strings name = "Alice" message = 'Hello, Python!'
-
Lists:
- Lists (list): Ordered collection of items, mutable (can be changed after creation).
Python Code:
# Lists numbers = [1, 2, 3, 4] fruits = ['apple', 'banana', 'orange']
-
Tuples:
- Tuples (tuple): Ordered collection of items, immutable (cannot be changed after creation).
Python Code:
# Tuples point = (10, 20) rgb_color = (255, 128, 0)
-
Dictionaries:
- Dictionaries (dict): Collection of key-value pairs, unordered, and mutable.
Python Code:
# Dictionaries person = {'name': 'John', 'age': 30, 'occupation': 'developer'}
- Python allows you to assign values of different data types to the same variable during runtime, whereas C# requires explicit declaration and enforces strict typing.
- Python uses different syntax for strings, lists, tuples, and dictionaries compared to C#.
Now that you've learned about Python's dynamic typing and common data types, you're ready to move on to Lesson 4, where we'll explore Python's control flow statements.
- Python Data Types: https://docs.python.org/3/library/stdtypes.html
- Python Lists, Tuples, and Dictionaries: https://docs.python.org/3/tutorial/introduction.html#lists
- Write a Python script that defines variables for different data types (numbers, strings, lists, tuples, and dictionaries). Print the values and their corresponding data types to the console.