🧩 Python Data Types and Variables

 

🔹 1. What Are Variables?
A variable is like a container that stores data in memory.
You can give it a name and assign a value.
Example:

x = 10
name = "Sakshi"
pi = 3.14
print(x, name, pi)
 

Rules for naming variables:
• Must start with a letter or underscore (_)
• Can contain letters, numbers, and underscores
• Case-sensitive (name ≠ Name)
• Cannot use Python keywords (like for, if, while)

 

🔹 2. Python Data Types
Python automatically detects the type of data (dynamic typing).

TypeExampleDescription
int10Whole numbers
float3.14Decimal numbers
str"Hello"Text data
boolTrue, FalseLogical values
list[1, 2, 3]Ordered, changeable collection
tuple(1, 2, 3)Ordered, unchangeable collection
set{1, 2, 3}Unordered, unique elements
dict{"name": "Sakshi", "age": 20}Key-value pairs

Example:

a = 10
b = 3.14
c = "Python"
d = True
e = [1, 2, 3]
f = (4, 5, 6)
g = {7, 8, 9}
h = {"name": "Sakshi", "age": 19}
 

🔹 3. Type Checking and Conversion
Check variable type:

x = 42
print(type(x))
 

Convert between types:

x = 10
y = float(x)
z = str(x)
print(y, z)
 

🔹 4. Getting User Input
Example:

name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "you are", age, "years old.")
 

🔹 5. Multiple Assignments
Example:

x, y, z = 10, 20, 30
print(x, y, z)

a = b = c = 0
 

🧠 Quick Practice Tasks

Simple Calculator – Take two numbers and print their sum, difference, product, and division.

User Info Display – Ask for name, age, and city; display in formatted output.

Data Type Identifier – Ask for user input and print its data type using type().