You are on page 1of 25

ELC 2414

Computer Programming
(2)
Lec 01: Revision (1)
Variables
Data Types
Type Example Usage
int x = 20 Integer numbers

float x = 1.45 Decimal point numbers

complex x = 5 +4j Complex number

str x = “Hello” , x = ‘Ahmed’ String of characters

bool x = True , x = False Boolean value (conditions)

None x = None An empty object (NULL)

tuple x = (6, 'tuple', 4 + 2j) An ordered sequence of items

list x = [1, 4, 5, 4] A list of elements


x= {‘a’: 56, ‘b’: 80, ‘c’:
dict 60}
A list of key/value pairs

A list of unordered unique


set 4/10/2022
x = {1, 4, 6, 9}
Computer Programming (2) elements 3
print()
• In python you can output (print) your result using print() statement.
• It can display strings, variables, operation results, or a mix of them.
•x = 9
•y = 7
• print('The following lines test Printing')
• print(x)
• print('Y value=', y)
• print('The sum=', x + y)
4/10/2022 Computer Programming (2) 4
Input
• Python provides command input() that receives input from user in a string
format.
• The command form as follows:
• Variable = input(Some prompt message)
• Example:
• age = input('Enter your age:')
• age = int(input('Enter your age:'))

4/10/2022 Computer Programming (2) 5


Operators
Arithmetic operators
Symbol Operation Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 5 / 2 2.5
% Mod 5 % 2 1
// Floor division 5 // 2 2
** Power 5 ** 2 25
4/10/2022 Computer Programming (2) 7
Comparison operators
Symbol Example True condition

== x == y Both have the same value

!= x != y Both have different value

> x > y x is greater than y

>= x >= y x is greater than or equal to y

< x < y x is smaller than y

<= x <= y x is smaller than or equal to y


4/10/2022 Computer Programming (2) 8
Logical operators

Symbol Example True condition

or x or y At least one is True

and x and y Both are True

not not x If x is False

4/10/2022 Computer Programming (2) 9


Example
• Write a Python program to calculate the area of a Triangle according to its
edges by the following law:
a+b+c
•s =
2
•area = s(s − a)(s − b)(s − c)

4/10/2022 Computer Programming (2) 10


Solution
• a = int(input('Enter the 1st edge: ‘))
• b = int(input('Enter the 2nd edge: ‘))
• c = int(input('Enter the 3rd edge: ‘))
• s = (a + b + c) / 2
• area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
• print('The area of the triangle is', area)
• Output
• Enter the 1st edge: 8
• Enter the 2nd edge: 6
• Enter the 3rd edge: 9
• The area of the triangle is 23.525252389719434

4/10/2022 Computer Programming (2) 11


Conditional statement
If Else

if condition:
statements to execute if
condition is True
else:
statements to execute if
condition is False
4/10/2022 Computer Programming (2) 13
Extending if-else blocks
if condition_1:
statements to execute if
condition_1 is True
elif condition_2:
statements to execute if
condition_2 is True
elif condition_n:
statements to execute if
condition_n is True
else:
statements to execute if
all above conditions are False
• elif = else + if which means that the previous statements must be false for the current one to evaluate to
true
4/10/2022 Computer Programming (2) 14
Example
• Calculate income tax for the given income according to the following
rules:

Income ($) Rate (%)


Less than 10000 0
From 10000 to 20000 10
Other 20

4/10/2022 Computer Programming (2) 15


Solution
• income = float(input('Enter your income: '))
if income < 10000:
tax = 0
elif income < 20000:
tax = income * 0.1
else:
tax = income * 0.2
print('Your income:', income, 'and payable tax:', tax)

4/10/2022 Computer Programming (2) 16


Loops
While loop
• A while statement iterates according to a condition.
• It doesn't run for a predefined number of iterations.

while condition:
perform a certain process

4/10/2022 Computer Programming (2) 18


Example
• Write a program that calculates the sum of numbers from 1 to a certain
number.
• n = int(input('Enter a number: '))
sum = i = 0
while i <= n:
sum += i
i += 1
print('Sum =', sum)

4/10/2022 Computer Programming (2) 19


break , continue
break continue
• Stops the current • Skips the current
iteration and all the iteration then resumes
next iterations. processing the next.
•c = 0 •c = 0
while c < 5: while c < 5:
c += 1 c += 1
if c == 3: if c == 3:
break continue
print(c) print(c)
print('end') print('end')

4/10/2022 Computer Programming (2) 20


For loop

for item in itemList:


perform something
on item
• For loop works either on a list of items or you can work on iterations using
range() function.

4/10/2022 Computer Programming (2) 21


Example
•for i in range(5):
print(i)
•Output
0
1
2
3
4
4/10/2022 Computer Programming (2) 22
Example
• for i in range(0, 100, 10):
print(i)
• Output
0
10
20
30
40
50
60
70
80
90
4/10/2022 Computer Programming (2) 23
Numerical Example
• Write a program that reads a number then prints numbers, squares, and
cubes of these numbers from 1 to the number.

4/10/2022 Computer Programming (2) 24


Solution
• n = int(input('Enter a number: '))
for x in range(1, n + 1):
sq = x ** 2
cb = x ** 3
print(f'Number={x}, Squared={sq}, Cubed={cb}')
• Output
• Enter a number: 5
• Number=1, Squared=1, Cubed=1
• Number=2, Squared=4, Cubed=8
• Number=3, Squared=9, Cubed=27
• Number=4, Squared=16, Cubed=64
• Number=5, Squared=25, Cubed=125

4/10/2022 Computer Programming (2) 25

You might also like