Variables in Python
Variables are fundamental elements in programming used to store data that can be referred to and manipulated in a program. In Python, variables are created when you assign a value to them, and they do not need an explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.
a = 100 (# a is variable here)
Declaring and Assigning variables:-
#declaring and assigning variables
age = 10
height= 6.1
name = "Pravesh"
is_roll_no = True
print("age:",age)
print("height",height)
print("name",name)
print("is_roll_no",is_roll_no)
Naming conversion:-
- "Please ensure that the variable names are descriptive."
- they must always start with a letter or can start with "_”, and can contain letters, numbers, and underscores.
- “Variable names are case-sensitive.”
valid variable names:-
_age = 10
name_of_student = "Pravesh"
roll_1 = True
print(_age)
print(name_of_student)
print(roll_1)
Invalid variable name:-
#invalid variable names
1age = 10
name-of-student = "Pravesh"
roll-1 = True
print(1age)
print(name-of-student)
print(roll-1)
it will give us an error.
Case Sensitive:-
#case sensitivity
name = "Jiger"
Name = "grewal"
print(name)
print(Name)
Variable Types:-
Python is a dynamically typed programming language, meaning the type of variables is determined at runtime.
#variables type
age = 5
name = "Pravesh"
height = 6.0
student =True
print("type of age is", type(age))
print("type of name is", type(name))
print("type of height is", type(height))
print("type of student is", type(student))
Type checking and conversion:-
we can change the datatypes of variables.
#type checking and conversion
a = 10
b = 20
print(type(a))
print(type(b))
a = str(a)
b = str(b)
print(type(a))
print(type(b))
age = “jiger”
print(int(age))
we can't change long name to int we will get an error
Dynamic typing:-
In Python, variables can change type during the execution of a program.
#dynamic typing
var = 10
print(var)
var = "Pravesh"
print(var)
we can do this multiple times.
Index:-
we have an amazing function in Python called input, which allows us to take user input.
#input
age = int(input("enter your age"))
print("your age is",age)
Python’s Gurus🚀
Thank you for being a part of the Python’s Gurus community!
Before you go:
- Be sure to clap x50 time and follow the writer ️👏️️
- Follow us: Newsletter
- Do you aspire to become a Guru too? Submit your best article or draft to reach our audience.