Python Data Types
Definition of data types
Categories of built-in data types in Python
Built-in Data Types
Text: str
Numeric: int, float, complex
Sequence: list, tuple, range
Mapping: dict
Set: set, frozenset
Boolean: bool
Binary: bytes, bytearray, memoryview
None: NoneType
Getting Data Type
Use type() function
Example:
x=5
print(type(x))
Setting Data Type
Data type is set when assigning a value to a variable
Python Numbers
Types: int, float, complex
Example:
x=1
y = 2.8
z = 1j
Integers (int)
Whole numbers without decimals
Examples:
1, -3255522, 35656222554887711
Python Variables
Variables store data values
Created when assigned a value
Creating Variables
x=5
y = 'John'
Variables can change type
Casting Variables
Specify data type using casting
str(), int(), float()
Variable Naming Rules
Start with letter or underscore
Cannot start with number
Only alphanumeric and _
Case-sensitive
Cannot use keywords
Variable Naming Styles
Camel Case: myVariableName
Pascal Case: MyVariableName
Snake Case: my_variable_name
Assign Multiple Values
x, y, z = 'Orange', 'Banana', 'Cherry'
x = y = z = 'Orange'
Unpacking Collections
fruits = ['apple','banana','cherry']
x, y, z = fruits
Output Variables
print(x)
print(x, y, z)
print(x + y + z)
Global Variables
Defined outside functions
Accessible everywhere
Local vs Global Variables
Local variables exist inside functions
Global variables remain unchanged outside