Python Notes
1. The Basics
What is Python? A simple, easy-to-read programming language used by NASA, Google, and
YouTube.
IDLE: The Integrated Development and Learning Environment where we write and run Python
code.
Case Sensitivity: Python sees Print() and print() as different. Always use lowercase for keywords.
2. Variables & Data Types
Variables are containers for storing data values.
Integer: Whole numbers (10).
Float: Decimal numbers (10.5).
String: Text inside quotes ("Skynet").
3. Operators: The Math Tools
Arithmetic: + (Add), - (Sub), * (Mul), / (Div), % (Remainder).
Comparison: == (Equal to), != (Not equal), > (Greater than), < (Less than).
4. Conditional Statements (Decision Making)
The if-else statement allows the computer to make decisions based on conditions.
5. Loops (Doing things again and again)
for loop: Used for a specific number of repetitions.
while loop: Used as long as a condition is true.
2. Variables & Data Types: The Storage System
Think of a Variable as a labeled box. You can put things inside it, change them later, or take them out to use
them.
Variables: A name (identifier) that stores a value.
o Rule: Must start with a letter or underscore _, never a number.
Data Types: The kind of data inside the box.
o Integer (int): Whole numbers without decimals. Example: goals = 3.
o Float: Numbers with decimal points. Example: price = 99.50.
o String (str): Text characters. Must be in quotes. Example: name = "Skynet".
3. Operators: The Action Tools
Operators are symbols that perform calculations or comparisons.
A. Arithmetic Operators (Math)
Used to perform mathematical calculations.
+ (Addition): 5 + 2 = 7
- (Subtraction): 5 - 2 = 3
* (Multiplication): 5 * 2 = 10
/ (Division): 5 / 2 = 2.5
% (Modulus): This is the "Remainder" provider. 5 % 2 = 1 (because 2 goes into 5 twice, with 1 left
over).
B. Comparison Operators (Logic)
Used to compare two values. The result is always True or False.
== (Equal to): 5 == 5 is True.
!= (Not equal to): 5 != 3 is True.
> (Greater than) / < (Less than): 10 > 5 is True.
4. Conditional Statements: The Decision Maker
The if-else statement is used when the program needs to choose between different paths based on a
condition.
How it works:
1. Check the if condition.
2. If True, run the code inside the if block.
3. If False, skip to the else block.
5. Loops: The Repetition Experts
Loops save you from writing the same code over and over again.
A. The for Loop
Used when you know exactly how many times you want to repeat a task.
Example: Printing your name 10 times.
Logic: It moves through a sequence (like a range of numbers).
B. The while Loop
Used when you want to repeat a task as long as a condition is True. You might not know exactly when it
will stop.
Example: "Keep running until you get tired."
Logic: Check condition Run Code Check condition again.
Summary Table for Revision
Feature Purpose Key Example
Variable Store Data score = 100
% Operator Find Remainder 7%3=1
if Make Decisions if age > 18:
for Set Repetition for i in range(5):
while Conditional Repetition while energy > 0:
🚫 Common Python Errors (And how to fix them)
1. SyntaxError: The Missing Colon
In Python, every if, else, elif, for, and while statement must end with a colon (:).
❌ Wrong: if age > 18
✅ Right: if age > 18:
2. IndentationError: The Space Trap
Python uses spaces (usually 4) to know which code belongs inside a loop or an if statement.
❌ Wrong:
Python
if True:
print("Hello") # This will cause an error!
✅ Right:
Python
if True:
print("Hello") # Notice the gap!
3. TypeError: Mixing Apples and Oranges
You cannot add a String to an Integer. If you take input, it is a string by default.
❌ Wrong: print("Score: " + 10)
✅ Right: print("Score:", 10) or print("Score: " + str(10))
4. NameError: The Typo
If you create a variable called score and try to print Score, Python will get confused because it is case-
sensitive.
❌ Wrong: points = 5, print(Points)
✅ Right: points = 5, print(points)
💡 Top 5 Exam Tips for Success
1. Dry Run: Before writing the final answer, trace the code on rough paper with a pencil. If it's a loop,
write down what the variable value is at every step.
2. Comment Your Code: Use the # symbol to explain your logic. Teachers love this!
o Example: # Checking if the number is even
3. Use Meaningful Names: Don't just use a, b, c. Use names like price, count, or username.
4. The input() Reminder: Always remember that input() returns text. If you are doing math, wrap it in
int() or float().
o Example: age = int(input("Enter age: "))
5. Check Your Quotes: Every opening " must have a closing ".
The "Broken Code" Challenge
Can you find the 3 errors in this script?
Python
number = input("Enter a number")
if number > 10
print("Big Number")
The Fix:
1. Line 1: number needs to be converted to an integer: int(input(...))
2. Line 2: Missing a colon : after the 10.
3. Line 3: Needs to be indented (pushed to the right).
📝 Practice Section
Part 1: Multiple Choice Questions (MCQs)
1. Who developed Python? (a) James Gosling (b) Guido van Rossum (c) Dennis Ritchie
2. Which symbol is used for comments? (a) // (b) /* (c) #
3. What is the output of print(10 % 3)? (a) 3 (b) 1 (c) 0.33
4. Which function takes input from the user? (a) get() (b) give() (c) input()
5. Which of these is a valid variable name? (a) 1_name (b) name_1 (c) name 1
6. What is the correct file extension for Python? (a) .py (b) .python (c) .pt
7. Which loop is used when we know the number of iterations? (a) while (b) for (c) do-while
8. What is 10 // 3? (a) 3.33 (b) 3 (c) 1
9. print("Hello" * 3) will result in: (a) Error (b) HelloHelloHello (c) Hello 3
10. Is Python an interpreted or compiled language? (a) Interpreted (b) Compiled (c) Both
Part 2: Fill in the Blanks
1. The ________ function displays output on the screen.
2. A ________ is a name given to a memory location that stores data.
3. In Python, ________ are used to define a block of code instead of curly braces.
4. The != operator stands for ________.
5. To convert a string input to an integer, we use the ________ function.
6. The range(5) function generates numbers from 0 to ________.
7. An if statement must end with a ________ symbol (:).
8. ________ loop repeats as long as a condition is True.
9. Data type for decimal numbers is ________.
10. True and False are known as ________ values.
Part 3: True or False
1. Python is a case-sensitive language. ( )
2. apple and Apple are the same variable names. ( )
3. A string must always be enclosed in quotes. ( )
4. Keywords in Python can be used as variable names. ( )
5. 5 + 2 * 3 will result in 21. ( )
6. Indentation is optional in Python. ( )
7. The % operator gives the quotient of a division. ( )
8. You can add an integer and a string directly (e.g., 5 + "apples"). ( )
9. Python is free and open-source. ( )
10. The else block is executed if the if condition is False. ( )
Part 4: Short Questions (2-3 Marks)
1. What is the difference between / and // operators?
2. Why is Python called a "high-level" language?
3. What is an Identifier? Give one example.
4. Define "Indentation" in Python.
5. What are Keywords? Name any two.
Part 5: Long Questions (5 Marks)
1. Explain the different Data Types available in Python with examples.
2. Describe the working of an if-elif-else ladder with a flowchart.
3. Compare the for loop and the while loop. When should you use which?
4. What are Operators? Explain Arithmetic and Comparison operators in detail.
5. How does Python handle user input? Explain with a small code example.
💻 Python Script Writing Questions
Try writing these on your own before checking the logic!
1. Write a program to find the area of a rectangle (Length × Width).
2. Write a program to check if a number entered by the user is Even or Odd.
3. Write a program to print the table of any number (e.g., table of 5).
4. Write a program to calculate the average of three subjects.
5. Write a program to find the largest of two numbers.
6. Write a program that asks for a name and greets the user (e.g., "Hello Skynet").
7. Write a program to check if a person is eligible to vote (Age >= 18).
8. Write a program to convert Temperature from Celsius to Fahrenheit.
9. Write a program to print the first 10 natural numbers using a for loop.
10. Write a program to calculate Simple Interest ().
Answer Key
MCQs: 1.b, 2.c, 3.b, 4.c, 5.b, 6.a, 7.b, 8.b, 9.b, 10.a
Fill in the Blanks: 1. print, 2. Variable, 3. Indentations/Spaces, 4. Not Equal To, 5. int(), 6. 4, 7. Colon, 8.
While, 9. Float, 10. Boolean
True/False: 1.T, 2.F, 3.T, 4.F, 5.F (BODMAS: 2*3=6, 6+5=11), 6.F, 7.F, 8.F, 9.T, 10.T
💻 Python Script Solutions
1. Area of a Rectangle
Python
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("The area is:", area)
2. Even or Odd
Python
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
3. Table of a Number
Python
num = int(input("Enter number for table: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
4. Average of Three Subjects
Python
s1 = float(input("Enter Marks 1: "))
s2 = float(input("Enter Marks 2: "))
s3 = float(input("Enter Marks 3: "))
avg = (s1 + s2 + s3) / 3
print("The average marks are:", avg)
5. Largest of Two Numbers
Python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print(a, "is larger")
elif b > a:
print(b, "is larger")
else:
print("Both are equal")
6. Simple Greeting
Python
name = input("What is your name? ")
print("Hello", name, "! Welcome to the world of Python.")
7. Voting Eligibility
Python
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote!")
else:
years_left = 18 - age
print("Too young. Wait", years_left, "more years.")
8. Celsius to Fahrenheit
Python
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(celsius, "C is equal to", fahrenheit, "F")
9. First 10 Natural Numbers
Python
print("First 10 Natural Numbers:")
for i in range(1, 11):
print(i)
10. Simple Interest Calculation
Python
p = float(input("Enter Principal: "))
r = float(input("Enter Rate: "))
t = float(input("Enter Time: "))
si = (p * r * t) / 100
print("The Simple Interest is:", si)
Part 4: Short Questions (2-3 Marks)
1. What is the difference between / and // operators?
/ (Division): This operator performs normal division and always returns a decimal (float) result. For
example, 5 / 2 results in 2.5.
// (Floor Division): This operator divides the numbers and rounds the result down to the nearest
whole number (integer). For example, 5 // 2 results in 2.
2. Why is Python called a "high-level" language? Python is called a high-level language because it is
designed to be user-friendly and easy for humans to read and write. It uses English-like words and doesn't
require the programmer to manage computer memory manually, unlike "low-level" languages (like Machine
Code) that the computer hardware understands directly.
3. What is an Identifier? Give one example. An identifier is a name used to identify a variable, function,
or other entities in a program.
Example: In price = 100, the word price is the identifier.
Rule: It must start with a letter or an underscore and cannot be a keyword.
4. Define "Indentation" in Python. Indentation refers to the spaces or tabs at the beginning of a line of
code. In most languages, indentation is just for neatness, but in Python, it is compulsory. It tells Python
which blocks of code belong to a specific loop or conditional statement.
5. What are Keywords? Name any two. Keywords are special reserved words that have a fixed meaning
in Python and cannot be used as variable names.
Examples: if, else, for, while, True, False.
Part 5: Long Questions (5 Marks)
1. Explain the different Data Types available in Python with examples. Python uses data types to
categorize different kinds of information:
Integer (int): Used for whole numbers. Example: age = 14
Float: Used for numbers with decimal points. Example: height = 5.8
String (str): Used for text, always enclosed in quotes. Example: city = "Delhi"
Boolean (bool): Used for logical values that are either True or False. Example: is_passed = True
2. Describe the working of an if-elif-else ladder with a flowchart. The if-elif-else ladder is used to check
multiple conditions one after another.
It starts with an if condition.
If that is false, it moves to the elif (else if).
If all conditions are false, the else block runs.
Example: ```python marks = 85 if marks > 90: print("A+") elif marks > 80: print("A") else:
print("B")
3. Compare the for loop and the while loop. When should you use which?
for loop: Used when you know the exact number of times you want to repeat a task. It usually
iterates over a range or a list.
o Use when: You want to print "Hello" 10 times.
while loop: Used when you want to repeat a task as long as a condition is True. You might not
know the exact number of repetitions beforehand.
o Use when: You want to ask a user for a password until they type the correct one.
4. What are Operators? Explain Arithmetic and Comparison operators in detail. Operators are symbols
that perform operations on values.
Arithmetic Operators: Used for mathematical calculations.
o + (Add), - (Subtract), * (Multiply), / (Divide), % (Modulus - gives remainder).
Comparison Operators: Used to compare two values, returning True or False.
o == (Equal to), != (Not equal to), > (Greater than), < (Less than).
5. How does Python handle user input? Explain with a small code example. Python handles user input
using the input() function. This function pauses the program and waits for the user to type something.
Note: By default, input() treats everything as a String. To do math, we must convert it using int() or
float().
Example:
Python
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Converting string to integer
print("Hello", name, "you are", age, "years old.")
💡 Pro-Tips for your Exam:
Indentation: Notice the 4 spaces inside if and for blocks. If you skip these, Python will give an
IndentationError.
Colon (:): Never forget the colon at the end of if, else, for, and while lines.
Data Types: Remember that input() always gives you a String. If you want to do math, you must use
int() or float().