Menu
Courses / python basic / Variables & Data Types

Variables & Data Types

03 / 10 Part of python basic





 

    📦 Topic 02 · Data
 


 


    Variables & Data Types
 


 


    Learn how Python stores and organises data using variables, and explore the core built-in types you'll use in every program.
 


 

    ⏱ ~35 min
    🟢 Beginner
    💡 Core concept
 


 
 

   

     
     

What is a Variable?


   

   


      A variable is a named container that holds a value. In Python you create one simply by assigning to it — no type declaration required. Python figures out the type automatically (this is called dynamic typing).
   


   

     

        variables.py
     

     
# Assigning variables — no type needed
name       = "Alice"
age        = 30
height     = 1.72
is_student = True

print(name, age, height, is_student)
# Alice 30 1.72 True

   

   

      📌 Naming Rules
     

Names must start with a letter or underscore, contain only letters/numbers/underscores, and are case-sensitive. my_var and My_Var are different variables.


   

 


 
 

   

     
     

Core Data Types


   

   


      Python has several built-in types. Use type() to inspect any value at runtime.
   


   
   

     
       
         
           
           
           
         
       
       
         
           
           
           
         
         
           
           
           
         
         
           
           
           
         
         
           
           
           
         
         
           
           
           
         
         
           
           
           
         
         
           
           
           
         
       
     
TypeExampleDescription
int42Whole numbers, positive or negative
float3.14Numbers with a decimal point
str"hello"Text — any sequence of characters
boolTrue / FalseBoolean logical values
NoneTypeNoneRepresents the absence of a value
list[1, 2, 3]Ordered, mutable collection
dict{"a": 1}Key-value pairs

   

   

     

        types.py
     

     
x    = 10
y    = 3.14
z    = "Python"
flag = False

print(type(x))    # <class 'int'>
print(type(y))    # <class 'float'>
print(type(z))    # <class 'str'>
print(type(flag)) # <class 'bool'>

   

 


 
 

   

     
     

Type Conversion


   

   


      Convert between types using built-in functions: int(), float(), str(), bool().
   


   

     

        conversion.py
     

     
age_str  = "25"
age_int  = int(age_str)    # "25" → 25

price     = 9.99
price_int = int(price)     # 9.99 → 9 (truncates)

count = 42
label = str(count)        # 42 → "42"

print(bool(0))           # False
print(bool(""))          # False
print(bool("hello"))    # True

   

 


 
 

   

     
     

Multiple Assignment


   

   


      Python supports concise multiple assignment on one line — handy for unpacking and swapping values without a temp variable.
   


   

     

        multi_assign.py
     

     
# Assign multiple variables at once
x, y, z = 1, 2, 3

# Swap without a temp variable
x, y = y, x
print(x, y)  # 2 1

# Same value to multiple names
a = b = c = 0