Python Tutorial : Variable
Python is one of the very popular programming language , it is beginner-friendly as well as professional-friendly and easy to understand .The first concepts every learner must understand is the variable.
A variable is identified by name , variable store
value in memory. In Python , variables store data , this data can be used or
can be manipulated later. Python does not require to declare a variable type
explicitly before assignment , it can be declared and assign dynamically.
Here is the example of variable
x = 10
y = "Hello World"
A variable is a musk like
a container that holds data. If we write x = 5
python creates an object with the value 5 and a
reference x that points to this object , then
x is not the value , x is a name pointing to the memory
location stored location.
Data Types of Variables
int |
--> Integer (full number or number with point) , |
|
x = 5 |
|
float--> |
Decimal number(number with point) |
, pi = 3.14 |
str--> |
String (text) ,name = "Hellow World" |
|
bool-->Boolean (True/False) , isHealthy=true
list-->Ordered,collection ,colors = ["red", "blue", "green"]
tuple-->Ordered, unchangeable collection,point = (10, 20)
dict-->Dictionary Objects ,Key-value pairs,student = {"name": "Ayan", "age": 22}
Example:
name = "Ayan" --string
_age = 25 --int
user_name1 = "admin" --string
isHealthy=true --boolean
my_dict = --dictionaly
{
"brand": "TATA",
"model": "KIA",
"year": 2024
}
class A: pass
class B: pass
class C: pass
lst = [A, B, C] --list
How to assign values to variables
Single assignment
Integer variables
age = 25
year = 2025
Float variables
pi = 3.14159
height = 5.8
temperature = -2.5
String variables
name = "Ayan Banerjee"
college = 'Netaji Subhash Engineering College'
course = "M.Tech in CSE"
Boolean variables
is_student = True
is_graduated = False
has_passed = True
List variables
colors = ["red", "green", "blue"]
numbers = [10, 20, 30, 40]
mixed = [1, "Ayan", True, 3.14]
Dictionary
student = {
"name": "Ayan",
"age": 25,
"course": "M.Tech CSE",
"is_active": True
}
Multiple Assignment in Python
Integer
a, b, c = 10, 20, 30
Float
p, q, r = 3.14, 2.71, 1.61
String
first_name, middle_name, last_name = "Ayan", "Kumar", "Banerjee"
Boolean
is_student, is_active, has_passed = True, True, False
List
colors, numbers, fruits = ["red", "green", "blue"], [1, 2, 3], ["apple", "banana"]
Dictionary
student1, student2, student3 = \
{"name": "Ayan", "age": 25}, \
{"name": "Riya", "age": 22}, \
{"name": "Arjun", "age": 23}
Dynamic Typing in Python
Python has a good feature of Dynamic Typing , than means , you do not do not need to declare the type of a variable explicitly,assign value directly , python will detect automatically variable type .Variable type can be change automatically during execution,which means , variable a is integer can be change to string by assign only
How python assign variable type automatically ?
x = 10
x = "Hello World"
How python change variable type ?
Float to Bool
y = 3.14 # float
print(y, type(y))
y = True # bool
print(y, type(y))
String to List
name = "Python"
print(name, type(name))
name = ["P", "y", "t", "h", "o", "n"]
print(name, type(name))
Boolean to String
flag = False
print(flag, type(flag))
flag = "False"
print(flag, type(flag))
What is the advantage of Dynamic Typing ?
- No need to declare types manually , development is fast.
- Variables can change any type of data during runtime.
- Clean and simple.
Deleting Variables
Deleting Variables in a smart feature of python , deleting is simple and it help free memory in long-running programs.
Delete a single variable
x = 10
del x
Delete multiple variables
a = 5
b = 10
c = 15
del a, b, c
Prepared By : Ayan Banerjee
No comments:
Post a Comment