1: Introduction to Python,

🐍 Post 1: Introduction to Python


Topics Covered:

  • Tokens: Keywords, Identifiers & Naming Rules, Operators

  • Data Types: Basic types, Mutable and Immutable

  • Input-Output Functions

🚩 What Are Tokens?

Tokens are the smallest building blocks of a Python program.
Main types:

  • Keywords: Reserved words with special meaning (e.g., if, else, while, for, def, class)

  • Identifiers: Names you give to variables, functions, classes, etc.

    • Rules:

      • Can use letters, digits, and underscores

      • Cannot start with a digit

      • Case-sensitive (Name and name are different)

  • Operators: Symbols that perform operations

    • Arithmetic: +, -, , /, //, %, *

    • Assignment: =, +=, -=, etc.

    • Comparison: ==, !=, <, >, <=, >=

    • Logical: and, or, not

🗂️ Data Types in Python

Python has several built-in data types.
Common types:

  • int (integer, e.g., 5)

  • float (decimal, e.g., 3.14)

  • str (string, e.g., "hello")

  • bool (True or False)

  • list (e.g., [1,- tuple (e.g., (1, 2, 3)`)

  • dict (dictionary, e.g., {"name": "Alice", "age": 20})

Mutable vs Immutable:

  • Mutable: Can change after creation (e.g., list, dict)

  • Immutable: Cannot change after creation (e.g., int, float, str, tuple)

🖥️ Input and Output in Python

  • Input: Use input() to get information from the user.
    Example:

    python

    name = input("Enter your name: ")

  • Output: Use print() to display information.
    Example:

    python

    print("Hello,", name)

💡 Example Code

python

# Getting user input and displaying output

name = input("Enter your name: ")

age = input("Enter your age: ")

print("Hello, " + name + "! You are " + age + " years old.")

📝 Practice Problem

Task:
Write a Python program that asks the user for their favorite color and then prints:
Your favorite color is {color}!

Starter code:

python

color = input("Enter your favorite color: ") print("Your favorite color is", color, "!")

🚀 Try It Out!

Use the interpreter below to practice:

1. Predict the Output: Variable Assignment

Question:
What will be printed by this code?

python

a = 5 b = a a = 10 print(b)

Walk-through:

  • a is set to 5.

  • b is set to a (so b is 5).

  • a is changed to 10, but b remains 5.
    Answer:
    5
    Concept: Variables store values, not references.

2. Mutable vs Immutable Types

Question:
What will be printed by this code?

python

x = [1, 2, 3] y = x x.append(4) print(y)

Walk-through:

  • x and y both refer to the same list object.

  • Modifying x also changes y.
    Answer:
    [1, 2, 3,[4]
    Concept: Lists are mutable and assignments copy references.

3. String Immutability

Question:
What does this code print?

python

s = "hello" t = s s = s.upper() print(t)

Walk-through:

  • t is assigned the value "hello".

  • s.upper() creates a new string; t is unchanged.
    Answer:
    hello
    Concept: Strings are immutable.

4. Operator Precedence

Question:
What will this code print?

python

result = 2 + 3 * 4 print(result)

Walk-through:

  • Multiplication happens before addition.

  • 3 * 4 = 12; then 2 + 12 = 14.
    Answer:
    14
    Concept: Operator precedence.

5. Conditional Logic

Question:
What will be printed?

python

x = 10 if x > 5: print("A") elif x > 0: print("B") else: print("C")

Walk-through:

  • x > 5 is True, so "A" is printed and the rest is skipped.
    Answer:
    A
    Concept: If-elif-else logic.

6. Loop with Range

Question:
How many times will "Hi" be printed?

python

for i in range(2, 10, 2): print("Hi")

Walk-through:

  • range(2, 10, 2) gives 2, 4, 6, 8 (four numbers).
    Answer:
    4 times
    Concept: Understanding the range function.

7. FizzBuzz Logic

Question:
What will be printed for i = 15 in this code?

python

i = 15

if i % 15 == 0:

print("FizzBuzz")

elif i % 3 == 0:

print("Fizz")

elif i % 5 == 0:

print("Buzz")

else: print(i)

Walk-through:

  • 15 % 15 == 0 is True, so "FizzBuzz" is printed.
    Answer:
    FizzBuzz
    Concept: Multiple conditions and order.

8. List Comprehension

Question:
What will this print?

python

nums = [1, 2, 3, 4] squares = [n**2 for n in nums] print(squares)

Walk-through:

  • Each number is squared: 1, 4, 9, 16.
    Answer:
    [1,[4][9][16]
    Concept: List comprehensions.

9. Dictionary Key Access

Question:
What will this code print?

python

d = {"a": 1, "b": 2} print(d.get("c", 3))

Walk-through:

  • .get("c", 3) returns 3 because "c" is not a key in the dictionary.
    Answer:
    3
    Concept: Dictionary .get() method and default values.

10. Function with Default Parameter

Question:
What is the output?

python

def greet(name="World"): print("Hello,", name) greet() greet("Alice")

Walk-through:

  • First call uses default, prints "Hello, World".

  • Second call passes "Alice", prints "Hello, Alice".
    Answer:

text

Hello, World Hello, Alice

Concept: Default function parameters.