A variable is a named storage location in memory used to store data. In Python, variables are dynamically typed, meaning you don’t need to declare their type explicitly.
Declaring Variables
x = 10 # Integer
name = "Dynamic Duniya" # String
price = 99.99 # FloatPython automatically determines the data type based on the assigned value.
if, else, while, etc.).age and Age are different variables).Valid examples:
my_var = 5
_name = "Dynamic Duniya"
value123 = 10.5Invalid examples:
3var = 10 # Invalid: cannot start with a number
my-var = 5 # Invalid: hyphens are not allowed
if = 20 # Invalid: 'if' is a reserved keywordPython has several built-in data types.
int): Whole numbers.float): Decimal numbers.complex): Numbers with real and imaginary parts.num1 = 10 # Integer
num2 = 20.5 # Float
num3 = 3 + 4j # Complex numberstr)Strings store text and are enclosed in single (' '), double (" "), or triple (''' or """) quotes.
text = "Hello, Python!"
multiline_text = """This is
a multiline string."""bool)Booleans hold True or False values.
is_active = True
is_logged_in = Falselist): Ordered, mutable collection.tuple): Ordered, immutable collection.range): Sequence of numbers.fruits = ["apple", "banana", "cherry"] # List
numbers = (1, 2, 3, 4) # Tuple
range_nums = range(1, 10, 2) # Range from 1 to 9 with step 2set): Unordered, unique elements.dict): Key-value pairs.unique_numbers = {1, 2, 3, 4, 5} # Set
student = {"name": "Rahul", "age": 25, "grade": "A"} # DictionaryTo check the type of a variable, use type():
print(type(10)) # <class 'int'>
print(type(10.5)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>Python allows converting between data types using type casting.
x = 5
y = 2.5
result = x + y # Python converts 'x' to float automatically
print(result) # 7.5
print(type(result)) # <class 'float'>a = 10
b = "20"
# Convert string to integer before addition
c = a + int(b)
print(c) # 30
# Convert integer to string
d = str(a) + b
print(d) # "1020"type() to check the data type.Sign in to join the discussion and post comments.
Sign inPython for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.
Object-Oriented Programming (OOP) in Python
Learn the fundamentals of Object-Oriented Programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and more. Understand how OOP enhances code reusability, scalability, and organization.