✍️Introduction
Python is one of the easiest programming languages to learn. Before writing real programs, you must understand variables, data types, and type conversion.
This article explains these concepts in simple English, with clear examples for beginners.
1️⃣ What is a Variable in Python?
A variable is a name that stores data in memory.
👉 Think of it like a container that holds a value.
Example:
name = "Tushar"
age = 22
marks = 85.5
Here:
- name stores text
- age stores a number
- marks stores a decimal value
✔ Python automatically understands the type of data
✔ No need to declare data type manually
2️⃣ Rules for Naming Variables
Follow these simple rules:
✅ Must start with a letter or underscore
✅ Can contain letters, numbers, underscore
❌ Cannot start with a number
❌ No spaces allowed
Correct:
student_name = "Rahul"
_marks = 90
3.1 Integer (int)
price = 99.99
percentage = 85.5
3.3 String (str)
Used for text data. Always written in quotes.
language = "Python"
message = "Hello World"
3.4 Boolean (bool)
Used for True or False values.
is_pass = True
is_fail = False
4️⃣ Check Data Type using type()
Python provides a built-in function to check data type.
x = 10
print(type(x))
Output:
<class 'int'>
5️⃣ Type Conversion in Python
Type conversion means changing one data type into another.
Example:
x = "10"
y = int(x)
print(y + 5)
Output:
15
Common Type Conversion Functions
Function Converts to
int() Integer
float() Float
str() String
bool() Boolean
Example:
a = 5
b = float(a)
c = str(a)
6️⃣ User Input and Type Conversion
By default, input() takes string input.
age = input("Enter your age: ")
print(type(age))
To convert it:
age = int(input("Enter your age: "))
print(age + 1)
7️⃣ Real-Life Example
Imagine an online exam system:
student_name = "Amit"
marks = 78
result = True
Here:
✔ Name → string
✔ Marks → integer
✔ Result → boolean
8️⃣ Common Beginner Mistakes ❌
- Forgetting type conversion
- Mixing string with numbers
- Wrong variable names
Wrong:
age = "20"
print(age + 5)
Correct:
age = int("20")
print(age + 5)
Conclusion
Understanding variables, data types, and type conversion is the foundation of Python programming.
Once you master this, learning loops, conditions, and functions becomes very easy.
👉 Practice daily and write small programs.
💬 Let’s Interact!
Do you think Python is good for beginners?
Comment YES or NO and tell us why 👇
📌 Related Articles:
Comments
Post a Comment