Learning Python : Day 2
Python Variables
In Python, a variable is a name given to a value. Variables are used to store and manipulate data in a program. Python has several types of variables, including:
1. Integers (int)
Integers are whole numbers, either positive, negative, or zero.
Example:
VerifyOpen In EditorEditCopy code1x = 5 # integer variable
2. Floats (float)
Floats are decimal numbers.
Example:
VerifyOpen In EditorEditCopy code1y = 3.14 # float variable
3. Strings (str)
Strings are sequences of characters, such as words or sentences. Strings can be enclosed in single quotes or double quotes.
Example:
VerifyOpen In EditorEditCopy code1name = 'John' # string variable
4. Boolean (bool)
Booleans are values that can be either True
or False
.
Example:
VerifyOpen In EditorEditCopy code1is_admin = True # boolean variable
5. List (list)
Lists are ordered collections of values. Lists can contain different data types, including strings, integers, and floats.
Example:
VerifyOpen In EditorEditCopy code1fruits = ['apple', 'banana', 'cherry'] # list variable
6. Tuple (tuple)
Tuples are ordered, immutable collections of values. Tuples are similar to lists, but they cannot be modified once created.
Example:
VerifyOpen In EditorEditCopy code1colors = ('red', 'green', 'blue') # tuple variable
7. Dictionary (dict)
Dictionaries are unordered collections of key-value pairs. Dictionaries are used to store and manipulate data in a program.
Example:
VerifyOpen In EditorEditCopy code1person = {'name': 'John', 'age': 30} # dictionary variable
Variable Assignment
In Python, variables are assigned using the assignment operator (=).
Example:
VerifyOpen In EditorEditCopy code1x = 5 # assign 5 to x
2y = x # assign the value of x to y
Variable Types
Python has several built-in functions to check the type of a variable:
type()
: returns the type of a variableisinstance()
: checks if a variable is an instance of a particular type
Example:
VerifyOpen In EditorEditCopy code1x = 5
2print(type(x)) # output: <class 'int'>
3print(isinstance(x, int)) # output: True
Variable Scope
In Python, variables have a scope, which determines where they can be accessed. There are two types of scope:
Local scope: variables defined inside a function or block are local to that scope
Global scope: variables defined outside a function or block are global and can be accessed from anywhere
Example:
VerifyOpen In EditorEditCopy code1x = 5 # global variable
2
3def my_function():
4 y = 10 # local variable
5 print(y) # output: 10
6
7print(x) # output: 5
8print(y) # error: y is not defined
Best Practices
Here are some best practices for working with variables in Python:
Use meaningful and descriptive variable names
Avoid using single-letter variable names, except for loop counters or temporary variables
Use consistent naming conventions throughout your code
Avoid using global variables, instead, pass variables as arguments to functions
Use type hints to specify the type of a variable
Multi Words Variable Names
# Camel Case
myVariableName = "John"
# Pascal Case
MyVariableName = "John"
# Snake Case
my_variable_name = "John"
Conclusion
In conclusion, understanding and effectively using variables is fundamental to programming in Python. By mastering different types of variables such as integers, floats, strings, booleans, lists, tuples, and dictionaries, you can store and manipulate data efficiently. Remember to follow best practices for variable naming and scope to write clean, readable, and maintainable code. With these skills, you'll be well-equipped to tackle more complex programming challenges in Python. Happy coding!