0% found this document useful (0 votes)
1 views10 pages

Python Variables and Data Types

The document provides a detailed explanation of Python variables and data types, emphasizing their importance in programming. It covers the definition of variables, how to use them, naming rules, and various data types including integers, floats, strings, and booleans. Additionally, it discusses dynamic typing, type conversion, and includes practice questions for reinforcement.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Python Variables and Data Types

The document provides a detailed explanation of Python variables and data types, emphasizing their importance in programming. It covers the definition of variables, how to use them, naming rules, and various data types including integers, floats, strings, and booleans. Additionally, it discusses dynamic typing, type conversion, and includes practice questions for reinforcement.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Variables and Data Types (Detailed

Explanation)
These are the foundation of Python programming. If you understand variables and data types
well, learning loops, functions, and other topics becomes much easier.

Part 1: What is a Variable?


A variable is a container that stores data in memory.

Real-Life Example

Think of a variable as a labeled box.

📦 Name Box → "Shuvo"


📦 Age Box → 21
📦 CGPA Box → 3.75

In Python:

name = "Shuvo"
age = 21
cgpa = 3.75

Here,

 name is a variable.
 age is a variable.
 cgpa is a variable.

The = sign is called the assignment operator. It stores the value on the right side into the
variable on the left side.

How Python Reads This


age = 21

Python does this:


1. Creates a variable named age
2. Stores the value 21
3. Remembers it for later use

Using Variables
name = "Shuvo"
age = 21

print(name)
print(age)

Output:

Shuvo
21

You can also combine text and variables:

name = "Shuvo"
age = 21

print("My name is", name)


print("I am", age, "years old")

Output:

My name is Shuvo
I am 21 years old

Variables Can Change


The word variable means the value can vary (change).

age = 21
print(age)

age = 22
print(age)

Output:

21
22
The old value is replaced by the new value.

Variable Naming Rules


✅ Valid Names
name = "Shuvo"
student_age = 21
cgpa1 = 3.75
_myname = "Shuvo"

❌ Invalid Names
1name = "Shuvo" # Cannot start with a number
my-name = "Shuvo" # Hyphen not allowed
class = "Python" # Reserved keyword

Naming Conventions
Good
student_name = "Shuvo"
total_marks = 450
building_height = 120

Bad
x = "Shuvo"
a = 450
b = 120

Good variable names make programs easier to understand.

Part 2: Data Types


A data type tells Python what kind of data is stored.

Think of data types as categories:


Data Type
25 Integer
3.75 Float
"Shuvo" String
True Boolean

1. Integer (int)
Stores whole numbers.

Examples:

age = 21
floor = 5
temperature = -10

Integers can be:

 Positive → 25
 Negative → -8
 Zero → 0

Integer Operations
a = 10
b = 5

print(a + b)
print(a - b)
print(a * b)

Output:

15
5
50

2. Float (float)
Stores decimal numbers.

Examples:
cgpa = 3.75
height = 5.8
pi = 3.14159

Float Operations
x = 5.5
y = 2.5

print(x + y)

Output:

8.0

3. String (str)
Stores text.

Strings are written inside quotes.

name = "Shuvo"
city = "Sylhet"
department = 'Architecture'

Accessing Characters
name = "Shuvo"

print(name[0])
print(name[1])

Output:

S
h

Python starts counting from 0.

S h u v o
0 1 2 3 4

String Concatenation
first = "Shuvo"
last = "Shill"

full = first + " " + last


print(full)

Output:

Shuvo Shill

String Repetition
print("Hi " * 3)

Output:

Hi Hi Hi

4. Boolean (bool)
Boolean has only two values:

True
False

Examples:

is_student = True
is_raining = False

Comparison Produces Boolean


print(10 > 5)
print(10 < 5)

Output:

True
False

Booleans are mainly used in conditions:

age = 20

if age >= 18:


print("Adult")
Checking Data Types
Python provides the type() function.

age = 21
cgpa = 3.75
name = "Shuvo"
student = True

print(type(age))
print(type(cgpa))
print(type(name))
print(type(student))

Output:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Dynamic Typing in Python


Python is dynamically typed.

You do not need to specify data types.

age = 21
name = "Shuvo"
cgpa = 3.75

Python automatically determines the data type.

Variables Can Change Type


x = 10
print(type(x))

x = "Hello"
print(type(x))

Output:
<class 'int'>
<class 'str'>

The same variable can hold different data types at different times.

Type Conversion (Casting)


Integer to Float
a = 10
b = float(a)

print(b)

Output:

10.0

Float to Integer
x = 3.75
y = int(x)

print(y)

Output:

The decimal part is removed.

String to Integer
age = "21"
age = int(age)

print(age + 5)

Output:

26
Integer to String
num = 100
text = str(num)

print("Number is " + text)

Output:

Number is 100

Summary Table
Data Type Example Description
int 25 Whole numbers
float 3.75 Decimal numbers
str "Shuvo" Text
bool True True or False

Architecture Examples
building_name = "Community Art Gallery" # str
floors = 4 # int
floor_height = 12.5 # float
has_basement = True # bool

Practice Questions
Q1

What is the output?

a = 10
b = 20
print(a + b)

Q2

What is the data type?

name = "Architecture"
Q3

What is the output?

x = 5.5
y = int(x)
print(y)

Try solving these yourself. After this, the next lesson should be Operators and Expressions,
because they are used everywhere in Python.

You might also like