This content originally appeared on DEV Community and was authored by Zakir Hussain Parrey
Python is an easy-to-learn, powerful programming language. One of the fundamental concepts every beginner should master is how variables work and the different data types available in Python.
What Are Variables?
A variable stores data that your program can use, modify, or display. In Python, you don’t have to declare the type of a variable explicitly. Python figures it out when you assign a value.
# Assigning values
name = "Alice"
age = 25
temperature = 36.6
Common Data Types in Python
-
String (
str
) Text data. Enclosed in single or double quotes.
greeting = "Hello, world!"
-
Integer (
int
) Whole numbers.
count = 42
-
Float (
float
) Numbers with decimals.
pi = 3.14159
-
Boolean (
bool
) Logical values:True
orFalse
.
is_active = True
- List Ordered, changeable collection of items.
fruits = ["apple", "banana", "cherry"]
-
Dictionary (
dict
) Key-value pairs.
student = {"name": "Bob", "age": 20}
Dynamic Typing
Python lets you change the type of a variable any time:
x = 5 # x is initially an integer
x = "five" # now x is a string
Conclusion
Understanding variables and data types is essential for writing effective Python programs. Practice by creating variables of different types and experimenting with them!
This content originally appeared on DEV Community and was authored by Zakir Hussain Parrey