Variables in Python serve as containers for storing data values. Assignment involves placing data into these containers using the equals sign (=). The left side of the assignment operator represents the variable name, while the right side contains the value being assigned.
Numeric Types
Integer Type
counter = 5
print(counter)
print(type(counter))
The variable counter can hold any integer value, which is why it's called a "variable". This code displays the stored value and its type.
The output will show <class 'int'>, indicating an integer type.
Variable Naming Conventions:
- Names can contain letters, digits, and underscores (_)
- Cannot begin with a digit
- Case-sensitive (myVar and myvar are dfiferent)
- Cannot use Python keywords
Python reserves certain identifiers with special functionality called keywords. These are already in use by the language and cannot be redefined by developers.
Common Python keywords include:
| and | as | assert | break | class |
| continue | def | del | elif | else |
| except | finally | for | from | global |
| if | import | in | is | lambda |
| nonlocal | not | or | pass | raise |
| return | try | while | with | yield |
| True | False | None |
Attempting to use these as variable names will result in errors.
In Python, when you assign a value to a variable, the system automatically determines the data type. You can check the type using type(variable_name). While Python doesn't require explicit type declarations, this automatic detection uses more memory.
Floating Point Type
measurement = 3.7
print(measurement)
print(type(measurement))
This assigns a floating-point value to measurement and displays both the value and its type.
The output shows <class 'float'> for floating-point numbers.
Complex Numbers
imaginary_value = 3+4j
print(imaginary_value)
print(type(imaginary_value))
This creates a complex number and identifies its type as <class 'complex'>.
String Type
text_one = 'python'
text_two = "python"
text_three = '''python'''
print(text_one)
print(text_two)
print(text_three)
Strings can be defined using single quotes ('), double quotes ("), or triple quotes ('''). All three variables store identical string data.
Boolean Type
Boolean values have only two possible states: True and False.
status = True
print(status)
print(type(status))
Booleans are commonly used in comparisons and conditional statements:
person_age = 16
is_adult = person_age >= 18
print('Adult status:', is_adult)
First, we define person_age as 16. The comparison person_age >= 18 evaluates to False, so is_adult receives the value False. The output will be Adult status: False.
List and Tuple Types
Lists and tuples can store multiple items of any data type. These items are called elements.
Lists
Lists use square brackets [...] and allow modification of their contents:
storage_list = [10, 'greeting']
print(storage_list)
Modifying list contents:
storage_list = [10, 'greeting']
storage_list.append(20)
storage_list.remove(10)
print(storage_list)
This adds element 20 and removes element 10, resulting in ['greeting', 20].
Tuples
Tuples use parentheses (...) and are immutable - their contents cannot be changed:
fixed_data = (10, 20, 'greeting')
print(fixed_data)
Attempting to modify a tuple results in an error:
fixed_data = (10, 20, 'greeting')
# fixed_data.append(30) # This would cause an AttributeError
Dictionary Type
Dictionaries are key-value mapping types enclosed in curly braces {...}. Keys are typically strings or numbers, while values can be any data type:
person_info = {"first_name": 'zhangsan', "years": 25, "gender": 'female'}
print(person_info)
Each dictionary element consists of a key-value pair. In "first_name": 'zhangsan', "first_name" is the key and 'zhangsan' is the value. Dictionaries allow modification of their key-value pairs.
None Type
empty_holder = None
print(empty_holder)
print(type(empty_holder))
None is a special constant representing the absence of a value. It belongs to the NoneType class and signifies "null" or "nothing". It's commmonly used for initializing variables that don't yet have meaningful values.