CS Grades 07_08_09 Mrs.
Wissam
Python programming
1. Python
Python is a high-level programming language used to write algorithms and programs
that can be executed by a computer.
It is:
➔ Interpreted (code is executed line by line)
➔ Text-based
➔ Case-sensitive
➔ Uses indentation to define program structure
2. Python Program Structure
A Python program is written as a sequence of statements, executed from top to
bottom.
Example:
print("Python")
print("Programming")
3. Keywords and Identifiers
➔ Keywords
Keywords are reserved words that have a special meaning in Python.
Examples:
if, else, for, in, range, True, False, print, input
➔ Identifiers
Identifiers are names given to variables.
Rules:
● Must start with a letter or underscore
● Cannot be a keyword
● No spaces
● Case-sensitive
1
CS Grades 07_08_09 [Link]
4. Comments
Comments are used to document code and are ignored by the interpreter.
# This is a comment
5. Variables
A variable is a named memory location used to store a value.
score = 75
name = "Sara"
➢ Python uses dynamic typing, meaning the data type is assigned automatically.
6. Data Types
Data Type Description Example
int Integer values 15
float Real numbers 3.14
str Sequence of characters "Python"
bool Logical values True / False
7. Input and Output
➔ Input
Used to receive data from the user.
age = int(input("Enter age: "))
➔ Output
print(age)
2
CS Grades 07_08_09 [Link]
8. Operators
➔ Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
9. Relational (Comparison) Operators
Operator Description
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater or equal
<= Less or equal
10. Logical Operators
Operator Function
and Both conditions must be true
or At least one condition true
not Reverses result
3
CS Grades 07_08_09 [Link]
11. Selection (Decision Making)
➔ if statement
➢ An if statement executes code only if a condition is true.
if score >= 50:
print("Pass")
➔ If–else
➢ Executes one block if the condition is true, otherwise another block.
if score >= 50:
print("Pass")
else:
print("Fail")
➔ If–elif–else
➢ Used when there are multiple conditions.
if score >= 80:
print("A")
elif score >= 60:
print("B")
else:
print("C")
4
CS Grades 07_08_09 [Link]
12. Iteration (Loops) [Grade 09]
➔ for loop
➢ A for loop repeats a block of code a fixed number of times.
➢ It is commonly used with range().
for i in range(1, 6):
print(i)
13. Strings
➢ A string is a sequence of characters.
➢ Strings are written inside double quotes.
text = "Computer Science"
14. Common String Functions
Function Purpose
len() Length of string
upper() Convert to uppercase
lower() Convert to lowercase
15. String Concatenation
➢ Concatenation joins strings together.
print("Hello " + name)
16. Indentation
➢ Indentation defines code blocks in Python.
➢ Incorrect indentation causes errors or wrong logic.
17. Algorithms and Python
➢ An algorithm is a step-by-step solution to a problem.
➢ Python is used to convert algorithms into working programs.