Variables & Data Types
1. Variables and Constants
Definition:
• Variable: A variable is a name that refers to a value stored in memory.
It is used to store data that can change during program execution.
• Constant: A constant is a variable whose value should not change once assigned.
Syntax:
variable_name = value
Example 1:
name = "Alice"
age = 25
print("Name:", name)
print("Age:", age)
Output:
Name: Alice
Age: 25
Example 2:
PI = 3.14159 # constant
radius = 5
area = PI * radius * radius
print("Area of Circle:", area)
Output:
Area of Circle: 78.53975
2. Data Types (int, float, str, bool)
Definition:
Python has different data types for different kinds of values:
• int → Integer numbers
• float → Decimal numbers
• str → String (text)
• bool → Boolean (True/False)
Syntax:
x = 10 # int
y = 5.5 # float
name = "Tom" # str
flag = True # bool
Example 1:
a = 10
b = 3.14
c = "Python"
d = True
print(type(a), type(b), type(c), type(d))
Output:
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
Example 2:
num1 = 15
num2 = 2.5
total = num1 + num2
print("Sum:", total)
Output:
Sum: 17.5
3. Type Conversion & Casting
Definition:
• Type Conversion: Automatic conversion done by Python (implicit).
• Type Casting: Manual conversion using functions like int(), float(), str(), bool().
Syntax:
int(value)
float(value)
str(value)
bool(value)
Example 1 – Implicit Conversion:
x=5
y = 2.5
z = x + y # int + float → float
print(z)
print(type(z))
Output:
7.5
<class 'float'>
Example 2 – Explicit Casting:
a = "100"
b = int(a) + 50
print("Result:", b)
Output:
Result: 150
4. Taking Multiple Inputs
Definition:
We can take multiple inputs in one line using the input() function and split() method.
Syntax:
a, b = input("Enter two numbers: ").split()
Example 1:
a, b = input("Enter two numbers: ").split()
a = int(a)
b = int(b)
print("Sum =", a + b)
Output:
Enter two numbers: 5 10
Sum = 15
Example 2:
name, age = input("Enter your name and age: ").split()
print("Name:", name)
print("Age:", age)
Output:
Enter your name and age: John 25
Name: John
Age: 25
5. type() and id() Functions
Definition:
• type() → returns the data type of a variable
• id() → returns the unique memory address of a variable
Syntax:
type(variable)
id(variable)
Example 1:
x = 10
print("Type of x:", type(x))
print("ID of x:", id(x))
Output:
Type of x: <class 'int'>
ID of x: 140709989945056 # (address will vary)
Example 2:
y = "Hello"
print("Type of y:", type(y))
print("ID of y:", id(y))
Output:
Type of y: <class 'str'>
ID of y: 140709989954960 # (address will vary)
Practice Programs
1. Swap Two Numbers
Program:
a = 10
b = 20
print("Before Swap: a =", a, ", b =", b)
a, b = b, a
print("After Swap: a =", a, ", b =", b)
Output:
Before Swap: a = 10 , b = 20
After Swap: a = 20 , b = 10
2. Simple Calculator
Program:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
Output:
Enter first number: 10
Enter second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0