lecture3
lecture3
Lectures Notes
For 1Styear, Civil Engineering, Faculty of Engineering, Al-Azhar
University
By, Dr. Eid F. Latif, PhD, Public Work Departement
Python number
Python offers various operations and functions to work with numbers.
a=5+3 #Addition
b=10-4 # Subtraction
c=4*6 # Multiplication
d=9/10 # Division
e=9/2 # Floor Division (//) divides and returns the whole number part of the
result (integer).
f=10 % 3 #Modulo (%) gives the remainder after a division operation.
g=5**2 #Exponentiation (**) raises a number to a power (=5^2)
h=(3+(4*5)) #Python follows the order of operations just like math
print(a, b, c, d, e, f, g, h)
a b c d e f g h
8 6 24 0.9 4.5 1 25 23
x=10
y=20
x=x+y or x +=y # x -=y # x *=y # x /=y ------etc
print(x)
Comparison Operations: These operations compare values and return
boolean (True or False)
== (equal to)
!= (not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
print(5 == 3) # False
age=26
country = "Egypt"
rank = 10
print(not age < 16 and country == "Egypt" or rank >0) # True
Other Number Functions:
#abs(x) returns the absolute value (positive version) of a
number.
print(abs(-5)) #5
#max(x, y,---) returns the larger of numbers.
print(max(5,3,7)) #7
#min(x, y, ----) returns the smaller of numbers.
print(min(5, 3,-1)) # -1
#round(x, n) rounds a number to a specified number of
decimal places (n defaults to 0).
print(round(3.14159, 2)) # 3.14
#pow(x, n) The pow(x, n) function in Python calculates the
power of a number, just like x**n.
print(pow(2,3)) #8
Import from python library
#from math import * : This will import all functions from the math module in
Python. The math module provides various mathematical functions,
from math import *
print(floor(3.7)) # returns the smallest integer number #3
print(ceil(3.7)) # returns the largest integer number #4
print(sqrt(4)) # calculates the square root of a number #2
print(pi) #3.141592653589793
print(radians(60)) #radians(x): Converts angle from degrees to radians.
print(sin(60))#sin(x): Returns the sine of x in radians.
print(log(5))#log(x, base=None): Returns the logarithm of x to the given
base. If base is not specified, returns the natural logarithm (base e).
print(log10(5))#log10(x): Returns the base-10 logarithm of x.=log(5,10)
If Conditional
Conditional statements, also known as if-else statements, are fundamental
control flow structures in Python. They allow your code to make decisions based
on whether a condition is True or False.
#Example:
age =int(input("how old are you?"))
is_adult = age >= 18 # True or False based on age
# Ternary conditional operator version
print("You are an adult" if is_adult else "You are not an adult")
# Output: "You are an adult"
#Example: ID
is_egyptian =input("Are you Egyptian? ‘yes’ or ‘no’ ").lower()
is_18=input("Are you above 18? Type ‘yes’ or ‘no’ ").lower()
print("Good, You can have an ID" if is_egyptian =="yes" and is_18== "yes"
else " Sorry, you have to be 18 or older and an Egyptian ID is give only to
Egyptians")