Types of Variable Names:
- Camel Case: First letter lowercase, subsequent words capitalized (e.g., myFirstVariable).
- Pascal Case: Each word starts with an uppercase letter (e.g., MyFirstVariable).
- Snake Case: Words separated by underscores, all lowercase (e.g., my_first_variable).
Multiple Values to Multiple Variables:
- Multiple values can be assigned to multiple variables in one line.
Program:
x, y, z = "cycle", "bike", "car" print(x) print(y) print(z)
Output:
cycle bike car
Assigning one value to multiple variables:
Program:
x = y = z = "Python" print(x) print(y) print(z)
Output:
Python Python Python
Unpack Collection:
- Collections like lists can also be unpacked into variables.
Program:
Vehicles = ["cycle", "bike", "car"] x, y, z = Vehicles print(x) print(y) print(z)
Output:
cycle bike car
Global Variables:
- Global variables are defined outside of functions and can be accessed inside functions.
- Use the global keyword to modify a global variable inside a function.
Program:
x = "easy" def myfunc(): global x print("Python is " + x) myfunc()
Output:
Python is easy
Local Variables:
- Local variables are defined inside a function and can only be accessed within that function.
Program:
def myfunc(): x = "easy" # Local variable print("Python is " + x) myfunc()
Output:
Python is easy
0 Comments