0% found this document useful (0 votes)
12 views20 pages

Python Programming Basics Guide

The document outlines an introductory session on Python programming, covering the main structure of a Python program, variable definitions, and rules for naming variables. It explains control flow using if statements and loops, including examples of nested ifs and for/while loops. Additionally, it discusses error handling with try-except blocks and provides an overview of useful operators.

Uploaded by

zfpmz6phgd
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)
12 views20 pages

Python Programming Basics Guide

The document outlines an introductory session on Python programming, covering the main structure of a Python program, variable definitions, and rules for naming variables. It explains control flow using if statements and loops, including examples of nested ifs and for/while loops. Additionally, it discusses error handling with try-except blocks and provides an overview of useful operators.

Uploaded by

zfpmz6phgd
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

PYTHON

By
Dr. Amr Galal
WHAT DO YOU
EXPECT FROM THIS
SESSION?
AGENDA
• Main structure of Python program.
• Variables.
• If condition
• Looping.
MAIN STRUCTURE OF PROGRAM
VARIABLE DEFINITION
VARIABLE NAMES RULES
• Must start with a letter or underscore
• Cannot start with a number
• Can contain letters, numbers, underscore
• Case-sensitive (Age ≠ age)
VARIABLE CHARACTERISTICS
• Can change variable type.
• Global variables and local variables.
• Variable shadowing.
IF
score = 75
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Needs improvement")
IF EXAMPLE
x = 10
if x > 5:
pass # placeholder for future code
else:
print("x is 5 or less")
NESTED IF
x = 10
y=5
if x > 0:
if y > 0:
print("Both numbers are positive")
LOOPING
• For
• While
LOOP CONTROLLING
• break
• Continue
• pass
FOR LOOP EXAMPLES
• for i in range(5):
print(i)

• for name in ["Ali", "Omar", "Sara"]:


print(name)
FOR LOOP EXAMPLES (CONT.)
for i in range(10): for i in range(5):
if i == 3: if i == 2:
break continue
print(i) print(i)
WHILE LOOP EXAMPLE
x=1
while x <= 5:
print(x)
x += 1
TRY, EXCEPT
try:

num1 = int(input("Enter numerator: "))

num2 = int(input("Enter denominator: "))

result = num1 / num2 # risky operation

except ZeroDivisionError:

print("Error: Cannot divide by zero!") # handles division by zero

except ValueError:

print("Error: Invalid input! Please enter numbers.") # handles non-integer input

else:

print(f"Result is: {result}") # runs only if no exception occurred

finally:

print("Operation completed.") # always runs


WHILE LOOP EXAMPLE
x=1
while x <= 5:
print(x)
x += 1
USEFUL OPERATORS
• Arithmetic: + - * / % ** //
• Comparison: == != > < >= <=
• Logical: and, or, not
• Membership: in, not in
Questions
Thank you

You might also like