Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 2(Variables and Data Types)

Updated
•2 min read•View as Markdown
S

Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey

Building skills in Python and full-stack development, with a focus on web apps and system design

Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering

From Python basics to full-stack projects, sharing my progress as I grow into a developer

Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development

01. What is a Variable?

  • A variable is like a label that stores data.

  • Use the = operator to assign a value.

x = 10          # Integer (int)
y = 3.14        # Floating-point number (float)
name = "Sabin"  # String (str)
is_ok = True    # Boolean (bool)

02. Basic Data Types (Primitive Data Types)

🔹 Integer (int)

  • Whole numbers without a decimal point
age = 25
print(age, type(age))  # 25 <class 'int'>

- Float (float)

  • Numbers with decimals
pi = 3.14159
print(pi, type(pi))  # 3.14159 <class 'float'>

- String (str)

  • A sequence of characters (use double " " or single ' ' quotes)
greeting = "Hello"
print(greeting, type(greeting))  # Hello <class 'str'>

- Boolean (bool)

  • Represents True or False
is_student = True
print(is_student, type(is_student))  # True <class 'bool'>

03. Variable Naming Rules

✅ Can contain letters, numbers, and underscores (_)
✅ Cannot start with a number
✅ Case-sensitive (Name ≠ name)
✅ Use descriptive names (recommended)

a = 10            # ❌ Not meaningful
student_age = 20  # ✅ Clear and descriptive

04. Type Casting

Convert between different data types.

# int → float
x = 10
print(float(x))  # 10.0
# float → int
y = 3.99
print(int(y))  # 3# str → int
num_str = "100"
print(int(num_str) + 1)  # 101# int → str
age = 25
print("Age: " + str(age))  # Age: 25

05. Practice Examples

📝 Example 1: Print Variables

name = "Ian"
age = 20
height = 189
is_student = False
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is student?", is_student)

📝 Example 2: Type Casting

a = "5"
b = 10
# Convert the string "5" into an int before adding
print(int(a) + b)  # 15

Python Basics

Part 2 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 3(Operators in Python)

Day 3 — Operators in Python 01. Arithmetic Operators Operator Meaning Example Result + Addition 5 + 2 7 - Subtraction 5 - 2 3 * Multiplication 5 * 2 10 / Division (float) 5 / 2 2.5 // Floor division 5 // 2 2 % Modulus (remainder) 5 % 2 1 ** Exponenti...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.