Chapter 1: Python Basics

The Computer Lab – Late Afternoon

Chaitanya sits at the terminal, staring at a black screen with a blinking cursor. He looks frustrated. Aditi Ma’am, the school’s System Architect, walks in holding a stack of manual attendance registers.

Chaitanya: Ma’am, I want to build the new School Management System you talked about, but this screen is empty. Where are the buttons?

Aditi Ma’am: We don’t need buttons yet, Chaitanya. We are starting with the Interactive Shell. Think of it as the “Reception Desk” of Python. You can ask it a quick question, and it answers immediately. You don’t need to fill out a whole form—just speak.

Chaitanya: So I just type?

Aditi Ma’am: Yes. This >>> prompt is Python waiting for you. Type 2 + 2 and hit Enter.

Chaitanya: (Typing)

Python

>>> 2 + 2
4

Chaitanya: Okay, it knows math.

Aditi Ma’am: That 2 + 2 is called an Expression.

  • The numbers 2 are Values.
  • The + is an Operator.
  • When you hit Enter, Python Evaluates it down to a single value: 4.

Chaitanya: What else can it do?

Aditi Ma’am: It follows the same BODMAS rules you learned in math class. Try this expression for the Sports Day budget calculation:

Python

>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0

Chaitanya: Wait, why 16.0? Why not just 16?

Aditi Ma’am: Good eye. That brings us to Data Types.

The Three Types of School Data

Aditi Ma’am: In our school records, we have different kinds of data: Roll Numbers, Percentages, and Names. Python categorizes them too.

1. Integers (int): Whole numbers.

  • School Example: Roll Numbers, Class Standard.
  • Python: 5, 42, -1.

2. Floating-Point Numbers (float): Numbers with decimals.

  • School Example: Temperature, Average Marks.
  • Python: 3.14, 98.5, 16.0.
  • Note: The division operator / always returns a float, even if the division is clean. That’s why you got 16.0.

3. Strings (str): Text.

  • School Example: Student Names, Remarks.
  • Python: 'Chaitanya', 'Pass', 'Fail'.

Chaitanya: I tried typing a name, but it failed.

Python

>>> Chaitanya
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Chaitanya' is not defined

Aditi Ma’am: You forgot the quotes! Without quotes (' '), Python thinks “Chaitanya” is a command or a variable it doesn’t know. With quotes, it knows it is just a piece of text.

String Arithmetic (The Glue)

Chaitanya: Can I add strings together? Like gluing two pages of a report?

Aditi Ma’am: Yes. This is called String Concatenation.

Python

>>> 'Aditi' + 'Maam'
'AditiMaam'

Chaitanya: It smashed them together.

Aditi Ma’am: Computers are literal. If you want a space, you must add it.

Python

>>> 'Aditi' + ' ' + 'Maam'
'Aditi Maam'

Chaitanya: What happens if I multiply a string? Like 'Hello' * 5?

Aditi Ma’am: Try it. This is called String Replication.

Python

>>> 'Hello' * 5
'HelloHelloHelloHelloHello'

Aditi Ma’am: But you cannot add a number to a string. You can’t say 'Chaitanya' + 5. That would be like asking me to add “Wednesday” to “5 Kilograms.” It makes no sense.

Variables (The School Lockers)

Chaitanya: Ma’am, the Interactive Shell is fun, but it has short-term memory. Once I calculate something, the answer is gone.

Aditi Ma’am: Then we need to store it. In Python, a Variable is like a school locker. You put a value inside, and you stick a label on the door.

Chaitanya: How do I label it?

Aditi Ma’am: We use an Assignment Statement with the = sign.

Python

>>> student_count = 40
>>> student_count
40

Aditi Ma’am: Now, the variable student_count holds the value 40. Unlike math, where = means “is equal to,” here it means “put the value on the right into the variable on the left.”

Chaitanya: What if a new student joins?

Aditi Ma’am: You just overwrite it.

Python

>>> student_count = 41

Aditi Ma’am: The old value is forgotten. The locker only holds one thing at a time.

Chaitanya: Can I name my variables anything? Can I name one Principal's Car?

Aditi Ma’am: There are three strict rules for naming variables:

  1. No spaces. (Use underscores like principal_car).
  2. No special characters (like !, @, #).
  3. Cannot start with a number. (1st_rank is illegal; rank_1 is fine).

Your First Program: The ID Card Generator

Aditi Ma’am: The shell is great for testing, but for the School System, we need to write a real program that we can save and run later. Open the File Editor.

Chaitanya: It’s just a blank notepad.

Aditi Ma’am: Type this in. We are going to make a program that interviews a new student for their ID card.

(Chaitanya types the following code carefully)

Python

# This program says hello and asks for my name.

print('Hello, School System!')
print('What is your name?')                       # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')                      # ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')

Aditi Ma’am: Save this as school_hello.py and press F5 to run it.

Chaitanya: It worked! It asked me my name, counted the letters, and told me how old I’ll be next year. But line 1 didn’t do anything.

Dissecting the Program

Aditi Ma’am: Let’s look at the logic board.

1. Comments (#) Aditi Ma’am: Line 1 starts with a #. Python ignores this. It’s a note for us humans.

2. The print() Function Aditi Ma’am: print('Hello') displays text on the screen. The parentheses () tell Python to execute the function.

3. The input() Function Aditi Ma’am: myName = input(). This stops the program and waits for you to type. Whatever you type—even if it’s a number—is stored as a String.

4. The len() Function Aditi Ma’am: This counts the characters in a string. 'Chaitanya' has 9 letters.

5. The Grand Finale (Type Casting) Aditi Ma’am: Look at the last line: str(int(myAge) + 1). This is the tricky part.

  • myAge is a string (e.g., '15'). You can’t add numbers to strings.
  • Step 1: int(myAge) converts '15' (text) to 15 (integer).
  • Step 2: 15 + 1 becomes 16.
  • Step 3: str(16) converts it back to text '16' so we can print it with the other words.

Chaitanya: So str(), int(), and float() are like translators?

Aditi Ma’am: Exactly. They translate data from one type to another so they can work together.

Aditi’s Final Quiz

Aditi Ma’am: Before you leave the lab, answer this. What does this code do?

Python

bacon = 20
bacon + 1
print(bacon)

Chaitanya: It prints 21?

Aditi Ma’am: No! It prints 20.

Chaitanya: Why?

Aditi Ma’am: Because on the second line, you calculated bacon + 1, but you didn’t store it anywhere. You didn’t say bacon = bacon + 1. The locker still holds 20.

Chaitanya: Ah! I have to be explicit.

Aditi Ma’am: Always. Now, go home and practice. Tomorrow, we learn how to make the computer make decisions.


Leave a Comment

💬 Join Telegram