Python Basics: Variables and Data Types in Depth ๐
๐ Understanding Variable Assignment
In Python, variables act as labels to store values, making your code organized and reusable. Assigning values is straightforward:
a = 3 # Variable 'a' now holds the value 3
Now, whenever you reference a
, Python knows it represents 3
. For example:
b = 4
print(a + b) # Output: 7
๐ง Variable Types and Reassigning Values
Python variables are dynamically typed, meaning the type is determined by the value you assign. For instance:
a = 3
print(type(a)) # Output: <class 'int'>
You can even reassign a different type to the same variable:
a = "Shivank"
print(type(a)) # Output: <class 'str'>
This flexibility is powerful but requires careful tracking of variable usage to avoid unexpected behavior.
๐ Data Types Overview
Hereโs a recap of common Python data types that variables can hold:
1๏ธโฃ Integers:
Whole numbers without decimals:
age = 25
print(type(age)) # Output: <class 'int'>
2๏ธโฃ Floats:
Numbers with decimals:
pi = 3.14159
print(type(pi)) # Output: <class 'float'>
3๏ธโฃ Strings:
Text enclosed in single or double quotes:
name = "Shivank"
print(type(name)) # Output: <class 'str'>
4๏ธโฃ Booleans:
True or False values:
is_active = True
print(type(is_active)) # Output: <class 'bool'>
๐ค Strings: Assigning and Combining
Strings are versatile and easy to assign:
greeting = "Hello"
print(greeting) # Output: Hello
To combine strings, use the +
operator:
first_name = "Shivank"
last_name = "Agarwal"
full_name = first_name + " " + last_name
print(full_name) # Output: Shivank Agarwal
Tip: Be consistent with quotes. Mixing single and double quotes like
greeting = 'Hello"
will throw an error.
๐ The Importance of Meaningful Variable Names
Readable code is maintainable code. Instead of using generic names like x
or y
, use descriptive variable names:
person_name = "Shivank"
person_age = 25
This practice helps others (and future you!) understand the purpose of each variable at a glance.
๐ Overwriting Variables
Python allows overwriting variables, even with different data types:
a = 3
print(a) # Output: 3
a = "Python"
print(a) # Output: Python
Caution: Overwriting variables unintentionally can lead to confusion, especially in larger projects.
โ Performing Operations on Variables
Pythonโs flexibility extends to variable operations. For numbers:
x = 10
y = 20
result = x + y
print(result) # Output: 30
For strings:
word1 = "Data"
word2 = "Science"
phrase = word1 + " " + word2
print(phrase) # Output: Data Science
๐ Debugging with the type()
Function
type()
FunctionTo avoid confusion, you can check the type of a variable using the type()
function:
x = 42
print(type(x)) # Output: <class 'int'>
x = "Hello"
print(type(x)) # Output: <class 'str'>
๐ ๏ธ Practical Example
Hereโs a combined example to reinforce these concepts:
# Assigning values
name = "Alex"
age = 30
is_coding = True
# Performing operations
message = name + " is " + str(age) + " years old."
print(message) # Output: Alex is 30 years old.
# Checking types
print(type(age)) # Output: <class 'int'>
print(type(is_coding)) # Output: <class 'bool'>
๐ Key Takeaways
Variables in Python hold values and make code manageable.
Python automatically infers a variableโs data type, but
type()
helps you confirm it.Use descriptive names for clarity and maintainability.
Python allows reassigning variables, even with different types.
Next Steps ๐ฎ
In the next section, weโll dive into control structures like loops and conditionals, unlocking Pythonโs full potential for building dynamic and interactive programs. Stay tuned! ๐
Last updated