Home HTML CSS JAVASCRIPT BOOTSTRAP Python Docker ML tutorial About us Privacy policy

Python Variables

Python Variables - CodesFamily

Python Variables

Variables in Python are used to store data, making it easy to manage and manipulate information in a program.

Declaring Variables

Python allows dynamic variable declaration without specifying a data type.


name = "Alice"
age = 25
height = 5.6
print(name, age, height)
        

Variable Types

Python supports multiple data types, such as strings, integers, floats, and booleans.


text = "Hello"
number = 10
decimal = 3.14
is_active = True
print(type(text), type(number), type(decimal), type(is_active))
        

Multiple Variable Assignment

You can assign multiple variables in one line.


x, y, z = 1, 2, 3
print(x, y, z)
        

Variable Scope

Python has local and global variable scopes.


def my_function():
    local_var = "I am local"
    print(local_var)

global_var = "I am global"
my_function()
print(global_var)
        

Constants

Though Python doesn’t have built-in constants, we use uppercase variable names by convention.


PI = 3.14159
GRAVITY = 9.8
print(PI, GRAVITY)
        

Conclusion

Variables are fundamental in Python. Understanding them is key to writing efficient programs. Next, we’ll explore data types in depth!