0% found this document useful (0 votes)
2 views5 pages

Python Study Guide

This Python Study Guide covers essential topics including data types, variables, arithmetic operators, input/output, and conditionals. Key concepts include identifying data types, naming conventions for variables, operator precedence, and the structure of if statements. The guide also highlights common mistakes and provides a cheat sheet for quick reference, along with the top five exam topics likely to be tested.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Study Guide

This Python Study Guide covers essential topics including data types, variables, arithmetic operators, input/output, and conditionals. Key concepts include identifying data types, naming conventions for variables, operator precedence, and the structure of if statements. The guide also highlights common mistakes and provides a cheat sheet for quick reference, along with the top five exam topics likely to be tested.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ EXAM TOMORROW

Python Study Guide


Weeks 3–5 · Data Types · Variables · Operators · I/O · Conditionals

1. DATA TYPES

■ Core Idea
A data type tells Python what kind of value a variable holds. Python has numeric and non-numeric types.

Numeric Types Non-Numeric Types

Type What it is Example Type What it is Example

int Whole numbers 26`, `-5`, `0str Text / characters "hello", 'hi'

float Decimal numbers 10.5`, bool True or False only True` /


`0.105e2 `False

complex Real + imaginary part 1 + 3.14j

Quick-Identify — From Exam Slides

Value Type Why

Total students in class int Counting whole people

Course code "IFY 2024" str Contains letters

Room temperature 27.5 float Has decimal

Student ID "M000123456" str Has letter M

Book price 25.99 float Has decimal

CGPA 2.6 float Has decimal

Day of week (Monday…) str Text

■■ Trick:
Even if an ID looks like a number (e.g. M000123), if it has letters → it's a str. You can't do math on it!

2. VARIABLES

■ Core Idea
A variable is a named space in memory that stores a value. Think of it as a labelled box.

Python Study Guide — Weeks 3–5 · Page 1


Naming Rules (must follow!) Valid / Invalid — From Exam Slides
• Must start with a letter (a–z, A–Z) or underscore _ Name Valid? Reason
• Can contain letters, numbers, and _
max_price ■ Yes snake_case, valid
• Case-sensitive — price ≠ PRICE
Total ■ No Space not allowed
• Cannot be a reserved word (print, int, if…)
students
• Can be any reasonable length
US$ ■ No $ symbol not allowed

student_id ■ Yes snake_case, valid

discount20 ■ Yes letters + numbers ok

1st_student ■ No Starts with number

ICT-2013 ■ No Hyphen not allowed

PEP 8 Naming Conventions

Type Convention Example

Variable lowercase with _ (snake_case) average_marks`, `area

Constant UPPERCASE with _ PI`, `MAX_LEVELS

Function lowercase with _ calculate_average()

■■ Common Mistakes:
Don't mix CamelCase and snake_case in the same program. Use meaningful names (first_name not x). Avoid
single chars l, O, I — look like 1 and 0.

3. ARITHMETIC OPERATORS & PRECEDENCE

All 7 Arithmetic Operators Precedence (PEMDAS / BODMAS)

Op Name Example Resul Priority Operator(s)


t
1 — Highest ( ) Parentheses/Brackets
+ Addition 5 + 3 8
2 ** Exponent
- Subtraction 5 - 3 2
3 * / % // (left to right)
* Multiplication 5 * 3 15
4 — Lowest + - (left to right)
/ Division 7 / 2 3.5
Worked Example from Slides
% Modulus (remainder) 7 % 2 1
x = 18 / 2 * 3 + 2
** Exponent (power) 2**3 8 18 / 2 = 9
9 * 3 = 27
// Floor division 7 // 2 3 27 + 2 = 29 ← answer

Precedence Exercises — From Exam Slides


Expression Step-by-step Answer

12 + 8 / 4 * 3 8/4=2, 2*3=6, 12+6 18

(12 + 8) / 4 * 3 (12+8)=20, 20/4=5, 5*3 15

11 // 3 + 3 ** (4-2) * 2 (4-2)=2, 3**2=9, 11//3=3, 9*2=18, 3+18 21

Python Study Guide — Weeks 3–5 · Page 2


■ Same-priority rule:
When * and / appear together go left to right. 6 / 3 * 2 = 4, but 6 / (3 * 2) = 1

4. INPUT, OUTPUT & ASSIGNMENT

Assignment Operator = input() — Read User Input


a = 5 # a is 5 ■■ CRITICAL: input() ALWAYS returns a string!
x = y = 10 # both 10
a, b = 10, 5 # a=10, b=5 name = input("Name: ") # str
s = 10 ** 3 # s=1000 age = input("Age: ")
tax = amount * 0.05 age = int(age) # str->int
price = float(input("Price: ")) # inline
print() — Display Output
Input → Process → Output Pattern
print("hello world")
print("hello ", "world") # hello world name = input("Enter name: ")
print("hello ", 2, " you")# hello 2 you age = int(input("Enter age: "))
print(5 + 2) # 7
print("5+20 =", 5 + 20) # 5+20 = 25 age_next = age + 1 # Process

print("Hi", name, "next year:", age_next)

Comments in Code
Inline Comment — same line, after 2+ spaces, # + Block Comment — before a section of code
space # Find the maximum mark
# Bad: obvious # from two given marks
area = l*w # length times width # and return the result
def max_mark(m1, m2):
# Good: explains intent if m1 > m2:
area = l*w # calculate rectangle area return m1
return m2

5. IF STATEMENTS & LOGICAL OPERATORS

if / else Structure Comparison Operators


if (condition):
Op Meaning Example → Result
Process 1 # runs when True
else: == Equal to `3 == 3` → True
Process 2 # runs when False
!= Not equal to `3 != 2` → True
Rules:
> Greater than `5 > 3` → True
• Use lowercase if and else
• End both lines with a colon : < Less than `2 < 5` → True

• else must align with if >= Greater or equal `3 >= 3` → True

• Indented block is the action to take <= Less or equal `4 <= 3` → False

Logical Operators — and / or

Python Study Guide — Weeks 3–5 · Page 3


C1 C2 C1 and C2 C1 or C2 Example — Age range check
age = int(input("Your age: "))
False False ■ False ■ False

False True ■ False ■ True if (age > 3 and age < 11):
print("Eligible to ride!")
True False ■ False ■ True else:
print("Not eligible")
True True ■ True ■ True
# Combining and + or:
and = BOTH must be True
if (c1) and (c2 or c3):
or = AT LEAST ONE must be True Process 1

Valid Conditions — From Exam Slides

Condition Valid? Reason

if (total = 500) ■ No Use == for comparison, not =

if (rate =>"FIVE") ■ No Wrong order: should be >= not =>; also mixing types

if (age = 2.5) ■ No Should be ==

if (mark =< 40) ■ No Wrong order: should be <= not =<

if (2.85 <= price) ■ Yes Valid comparison

if (5+8) ■ Yes Non-zero is truthy in Python

if ((1-1) <= 0) ■ Yes 0 <= 0 is True

if (ticketType == "Golden Class") ■ Yes Correct string comparison

■■ String comparison is case-sensitive!


"Abu Dhabi" == "abu dhabi" → False (capital letters matter!)

6. QUICK CHEAT SHEET

Data Types Input / Output


int -> whole numbers (26, -5) x = input("msg") # str!
float -> decimals (10.5, 3.14) x = int(input("msg")) # int
str -> text ("hello") x = float(input("msg")) # float
bool -> True / False print("text", variable)

Arithmetic Operators Conditionals


+ - * / -> basic math if (condition):
% -> remainder (7%2 = 1) # True branch
** -> power (2**3 = 8) else:
// -> floor div (7//2 = 3) # False branch

Precedence (high → low) # operators: == != > < >= <=


1. ( ) parentheses # logical: and or
2. ** exponent
3. * / % // left to right if (a > 3 and a < 11):
4. + - left to right print("in range")

Python Study Guide — Weeks 3–5 · Page 4


■ TOP 5 MOST LIKELY EXAM TOPICS

1. Identify the correct data type for a given value


2. Spot an invalid variable name and explain why
3. Evaluate an arithmetic expression step-by-step using operator precedence
4. Fix a broken if-statement (e.g. = instead of ==, or wrong operator order)
5. Write a short program using input(), type conversion, calculation, and print()

Python Study Guide — Weeks 3–5 · Page 5

You might also like