In Python, variables are used to store information to be referenced and manipulated in a program. They act as placeholders for values.
A variable can hold different types of values, like numbers, strings, or even more complex data structures like lists and dictionaries.
1. Creating Variables
You create a variable by assigning a value using the = sign.
# Assigning integer value to a variable age = 25 # Assigning string value to a variable name = "Alice" # Assigning float value to a variable height = 5.9 # Assigning boolean value to a variable is_student = True # Printing variables print(age, name, height, is_student)
Explanation:
age, name, height, and is_student are variables.
Variables can store different types of data: integers, strings, floating-point numbers, and booleans.
2. Variable Naming Rules
When naming variables, there are some basic rules:
Variable names can only contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
They must start with a letter or an underscore but not a number.
Python is case-sensitive, so myVar and myvar are different variables.
Examples:
# Valid variable names my_var = 10 _var = 20 myVar = 30 age2 = 25 # Invalid variable names # 2var = 5 # Cannot start with a number # my-var = 6 # Dashes are not allowed in variable names
3. Dynamic Typing
Python is a dynamically typed language, which means you don't need to declare the type of a variable. Python determines the type of the variable at runtime.
x = 10 # x is an integer x = "Hello" # x is now a string
In the above example, the variable x initially holds an integer value, and later it holds a string value. Python does not require explicit type declaration.
4. Multiple Assignments
You can assign values to multiple variables in one line.
# Assigning the same value to multiple variables a = b = c = 5 print(a, b, c) # Assigning different values to multiple variables x, y, z = 1, "hello", 3.5 print(x, y, z)
5. Swapping Variables
Python allows you to easily swap the values of two variables without needing a temporary variable.
# Before swapping a = 10 b = 20 print("Before swapping:", a, b) # Swapping a, b = b, a # After swapping print("After swapping:", a, b)
6. Type Casting
You can convert the value of one data type into another, a process known as type casting.
Examples:
# Integer to float num = 5 float_num = float(num) print(float_num) # Output: 5.0 # Float to integer height = 6.8 int_height = int(height) print(int_height) # Output: 6 # Integer to string age = 25 str_age = str(age) print(str_age) # Output: "25"
7. Variable Scope
A variable's scope defines where in the program it can be accessed. There are two main scopes:
Local Scope: Variables created inside a function.
Global Scope: Variables created outside of a function.
Example:
# Global variable x = "global variable" def my_function(): # Local variable x = "local variable" print(x) my_function() # Output: "local variable" print(x) # Output: "global variable"
8. Global Keyword
If you want to modify a global variable inside a function, you must declare it using the global keyword.
x = 5 def modify_global(): global x x = 10 modify_global() print(x) # Output: 10
9. Deleting Variables
You can delete a variable using the del keyword.
x = 10 del x # Trying to print x will raise an error # print(x) # NameError: name 'x' is not defined
10. Constants in Python
In Python, constants are usually defined in all caps to differentiate them from variables. Although Python does not enforce constants, this naming convention is widely accepted.
PI = 3.14159 GRAVITY = 9.8 print(PI, GRAVITY)
11. Best Practices for Variable Naming
Use descriptive names: It’s better to name variables based on what they represent rather than using generic names like x or a.
Use underscores for readability: If a variable name is long, use underscores to make it easier to read.
Example:
# Less descriptive x = 100 # More descriptive total_price = 100
Recap:
Variables store data and can hold different types of values.
Python uses dynamic typing, so you don’t need to declare types explicitly.
Variables can be reassigned, swapped, and even deleted.
It is crucial to follow naming conventions and understand the scope of variables to write clear and error-free code.
By mastering these basics, you'll have a strong foundation for writing Python programs!