- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Introduction:
Variables are an important concept in any programming language, including Python. In simple language, a variable is a named location in memory that stores a value. In Python, you can use variables to store any type of data, including numbers, strings, and even complex data structures like lists and dictionaries.
Creating Variables in Python:
To create a variable in Python, you need to give it a name and assign a value to it. Here’s an example:
Name = “Omkar” Age = 25 Profession = “Software Engineer “
Python Variable Naming Rules:
When creating variables in Python, there are a few rules that you need to follow:
1). Variable names must start with a letter or underscore (_), followed by any combination of letters, digits, and underscores.
2). Variable names are case-sensitive, which means name and Name are two different variables.
3). A variable name cannot start with a number.
4). A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ ).
Python Variable Types:
1). Numbers: Consists of integers, floating-point numbers, and complex numbers.
2). Strings: Consist of characters in quotes.
3). Booleans: Consist of True or False values.
4). Lists: Consist of ordered sequences of elements.
5). Tuples: Consist of ordered, immutable sequences of elements.
6). Sets: Consist of unordered collections of unique elements.
7). Dictionaries – Consist of unordered collections of key-value pairs.
Checking Types of the Variable:
First, we will create some variables.
Name = “ Omkar “
Age = 25
Height = 5.8 ft
To check the type of the variable use the type() function.
print(type(Name)) # Output: <class ‘str’> print(type(Age)) # Output: <class ‘int’> print(type(Height)) # Output: <class ‘float’>
To check the address of the variable, use the id() function.
print(id(Name)) # Output: 2623848788368
print(id(Age)) # Output: 140718085367336
print(id(Height)) # Output: 2623846066576
Value Assignment:
To assign the value to a variable, use the equal sign (=).
Example:
A = 10
Assign the same value to multiple variables.
A = B = C = 10
Multiple Value Assignment:
We can assign different values to different variables at the same time. We separate the variables and their values by commas ‘,’.
Example:
a, b, c = 40, 50, 60
Here,
Value of variable a is 40 , b is 50 and c is 60.
Scope of Variable:
In Python, the scope of a variable determines where in the code the variable can be accessed and used. The scope of a variable is defined by where the variable is created and assigned a value.
There are two types of variable scopes in Python: global scope and local scope.
1). Global Scope:
Variables created outside of any function or class have a global scope. This means that they can be accessed and modified from anywhere in the code, including within functions and classes.
Example:
number = 100# global variable
def print_number():
print(number) # accessing global variable
def modify_number():
global number # declaring number as global variable
number = 200 # modifying global variable
print_number() # Output: 100
modify_number()
print_number() # Output: 200
In the example above, the variable number is declared outside of any function, so it has a global scope. The function print_number() can access and print the value of number, and the function modify_number() can modify the value of number by declaring it as a global variable using the global keyword.
2). Local Scope:
Variables created inside a function or class have a local scope. This means that they can only be accessed and modified within that function or class.
Example:
def my_function():
number = 100 # local variable
print(number) # accessing local variable
my_function() # Output: 100
print(number) # NameError: name 'number' is not defined
Basic Mathematical Operations Using Variables:
Creating variables and assigning values to them:
a, b = 10 , 20
Performing operations and printing output:
print(a+b) # Addition Output: 30
print(a-b) # Subtraction Output: -10
print(a*b) # Multiplication Output: 200
print(a/b) # Division Output: 0.5