Python Variables and Data Types - Notes
1. Variables in Python
• Variables are containers used to store data values.
• You don’t need to declare the type; Python determines it automatically (dynamic typing).
Rules for Naming Variables:
• Must start with a letter or underscore (_).
• Cannot start with a number.
• Can contain letters, digits, and underscores.
• Case sensitive (age, Age, AGE are different).
Examples:
x = 10
name = "Sumedh"
_price = 99.5
2. Data Types in Python
Python has several built-in data types:
Numeric Types:
• int – whole numbers (10, -5)
• float – decimal numbers (3.14, -0.5)
• complex – complex numbers (2+3j)
Text Type:
• str – string values ("Hello", 'Python')
Sequence Types:
• list – ordered, mutable ( [1, 2, 3] )
• tuple – ordered, immutable ( (1, 2, 3) )
• range – sequence of numbers ( range(5) )
Mapping Type:
• dict – key-value pairs ( {"name": "Sumedh", "age": 20} )
Set Types:
• set – unordered collection of unique items ( {1, 2, 3} )
• frozenset – immutable set
Boolean Type:
• bool – True or False
Binary Types:
• bytes, bytearray, memoryview
3. Type Conversion
Convert values using built-in functions:
• int(), float(), str(), list(), tuple(), set()
4. Checking Data Type
Use the type() function:
• type(10) → int
• type("Hello") → str
5. Dynamic Typing
Variables can change their type at runtime:
x = 10
x = "Now I'm a string"
These notes cover the basics needed for beginners learning Python.