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

Programming Web by Python Part 2

The document provides an overview of Python operators, including arithmetic, comparison, and assignment operators, along with examples of their usage. It also covers type conversion and the input function, explaining how to read user input and convert data types. Additionally, it includes homework exercises to practice these concepts.

Uploaded by

kanaanadaba68
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 views46 pages

Programming Web by Python Part 2

The document provides an overview of Python operators, including arithmetic, comparison, and assignment operators, along with examples of their usage. It also covers type conversion and the input function, explaining how to read user input and convert data types. Additionally, it includes homework exercises to practice these concepts.

Uploaded by

kanaanadaba68
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

ADEL AHMED ALHAJJ

Type( ) Function

2
‫‪Python Operators‬‬

‫التعليقات العمليات الحسابية في ‪: Python‬‬

‫‪ -1‬عملية الجمع‬

‫‪3‬‬
‫‪Python Operators‬‬

‫التعليقات العمليات الحسابية في ‪: Python‬‬

‫‪ -2‬عملية الضرب‬

‫‪4‬‬
‫‪Python Operators‬‬

‫التعليقات العمليات الحسابية في ‪: Python‬‬

‫‪ -3‬عملية الطرح‬

‫‪5‬‬
‫‪Python Operators‬‬

‫التعليقات العمليات الحسابية في ‪: Python‬‬

‫‪ -4‬عملية القسمة‬

‫‪6‬‬
Python Operators >> Arithmetic operators

Operation Function Example in Python

Addition: Sum of two + [Link](a,b) x = 5; y = 6 x + y = 11


operands import operator
[Link](5,6) 11

Subtraction: Left operand - [Link](a,b) x = 10; y = 5 x - y =5

minus right operand import operator


[Link](10, 5) 5

Multiplication * [Link](a,b) x = 5; y = 6 x * y=30


import operator
[Link](5,6) 30

7
Python Operators >> Arithmetic operators

Operation Function Example in Python


Division / [Link](a,b) x = 6; y = 3 x / y= 2
import operator
[Link](6,3) 2
Modulus: Reminder of a/b % [Link](a, b) x = 11; y = 2 x % y= 1
import operator
[Link](11, 5) 1

Exponentiation: Left ** [Link](a,b) x = 2; y = 3 x ** y= 8

operand raised to the import operator

power of right [Link](2, 3) 8

8
Python Operators >> Arithmetic operators

9
Python Input( ) Function

10
Example 1

Q1: Write program to find area for the circle of radius

the radius = 20 >>> area= radius * radius * 3.14

ADEL AHMED ALHAJJ


11
Example 2

Q2: Write program to find y = 7x + 20 >> x = 4

ADEL AHMED ALHAJJ


12
Example 3

Q3: Write program to find y = 7x +b + 3a >> x=4 b=2 a=10

ADEL AHMED ALHAJJ


13
Example 4

Q4: Write program to find y= 5x + 2 + m / 2w * m >>

x=3 m=5 w=2

ADEL AHMED ALHAJJ


14
H . W

y=14x+12b/7

ADEL AHMED ALHAJJ


a= 5 + x + y
z 2.7 w

4.5( x + 2.3 y ) 2
b=
z+w

15
Python Type Conversion

Converting Between Data Types

Converting Integers to Floating-Point Numbers f = 57


(Decimals) print( float(f) )
The float() function converts integers to floating-point
numbers (decimals). To use this function, place an
integer inside the parentheses.

Converting Floating-Point Numbers (Decimals) to


b = 125.0
Integers
c = 390.8
The int() function converts floating-point numbers
print( int(b) )
(decimals) to integers. To use this function, place the
print( int(c) )
decimal number inside the parentheses.

16
Python Type Conversion

Converting Between Data Types

Converting Numbers to Strings

We can convert numbers to strings using the str() function.

v = 32
print( str (v) )

f = 524.53
print ("Sammy has " + str(f) + " points.")

17
Python Type Conversion

18
Python Type Conversion

19
Python Type Conversion

20
Python Type Conversion

21
Python Input( ) Function

Allowing the User to Input Data into the Program While It Is Running

The input() function reads the user's input and returns it as a string

We use a built-in function called input().

Each time you call this function, the Python interpreter waits for you to enter
something from the keyboard. After you finish entering and press the Enter key,
whatever you typed will be returned as a string at the location where
the input() function was called.

Note:

When you call the input() function, even if you enter a number, it will still return it
as a string.

Therefore, if you want the user to enter a number, you will need to convert what
the function returns into a number.

22
Python Input( ) Function

23
Python Input( ) Function

24
Python Input Function

25
Python Input Function

26
Python Input Function

27
Python Input Function

Q: Write program to Reading three numbers input from keyboard


.The program reads three numbers and displays their Sum and

ADEL AHMED ALHAJJ


Average

Three numbers >> Assume a ,b , and c

Sum and Average >> Assume Sum and Averg

Sum = a + b + c

Averg= Sum / 3

28
Python Input Function

ADEL AHMED ALHAJJ


29
Python Input Function

ADEL AHMED ALHAJJ


30
H . W

Q1- Write a program that inputs two numbers and then multiplies them?

Q2- Write a program that inputs a number and then prints the double of
that number?

Q3- Write a program that inputs 4 values, then finds their sum and
average.

Q4- Write a program that inputs two numbers and displays:

Their sum , Their difference , Their product , Their division result

Q5- Write a program that asks the user for their first name and last name
(as strings), then prints their full name

31
‫‪Booleans Operations‬‬ ‫المنطقية –‬ ‫العمليات‬

‫‪ comparing‬المقارنة‬

‫هي عملية منطقية تكون ناتجها اما صواب ‪ True‬او خطأ ‪False‬‬
‫ديبل يساوي وهي لمقارنة الطرف األيمن يساوي الطرف االيسر‬
‫==‬
‫‪.‬‬
‫ال يساوي لعمل مقارنة الطرف األيمن ال يساوي الطرف االيسر‬ ‫!=‬
‫اصغر من وهي لمقارنة اذا كانت القيمة االولى اصغر من القيمة الثانية‬ ‫<‬
‫اكبر من وهي لمقارنة اذا القيمة االولى اكبر من القيمة الثانية‬ ‫>‬
‫اصغر من او يساوي وهي لمقارنة اذا كانت القيمة االولى اصغر من او تساوي القيمة‬ ‫<=‬
‫الثانية‬

‫اكبر من او يساوي وهي لمقارنة اذا كانت القيمة االولى اكبر من او تساوي القيمة الثانية‬
‫>=‬
‫‪32‬‬
Booleans Operations – ‫المنطقية‬ ‫العمليات‬

33
Python Operators >> Comparison Operators

Function Description Example in Python

> [Link](a,b) True if the left operand is higher x = 5; y = 6 x > y

than the right one import operator


[Link](5,6)

< [Link](a,b) True if the left operand is lower x = 5; y = 6 x < y

than right one import operator


[Link](5,6)

== [Link](a,b) True if the operands are equal x = 5; y = 6 x == y


import operator
[Link](5,6)

34
Python Operators >> Comparison Operators

Function Description Example in Python

=! [Link](a,b) True if the operands are not equal x = 5; y = 6 x != y


import operator
[Link](5,6)

=< [Link](a,b) True if the left operand is higher than x = 5; y = 6 x >= y

or equal to the right one import operator


[Link](5,6)

=> [Link](a,b) True if the left operand is lower than x = 5; y = 6 x <= y

or equal to the right one import operator


[Link](5,6)

35
Python Operators >> Comparison Operators

36
Booleans Operations – ‫المنطقية‬ ‫العمليات‬

and , or , not ‫يمكن ربط اكثر من شرط والتحقق من اكثر من شرط من خالل الكلمات المحجوزة‬
.

A B A and B

True True True

True False False

False True False

False False False

37
Booleans Operations – ‫المنطقية‬ ‫العمليات‬

A B A OR B
.
True True True

True False True

False True True

False False False

A NOT A

True False

False True 38
Booleans Operations – ‫المنطقية‬ ‫العمليات‬

39
Assignment Operations

40
Assignment Operations

41
Assignment Operations

42
Assignment Operations

43
Python Operators >> Assignment Operators

Operator Function Example in Python

+= [Link](a,b) x=5 x += 5 >> 10


import operator x = [Link] (5, 5)

-= [Link](a,b) x=5 x - = 2 >>3


import operator x = [Link](5,2)

*= [Link](a,b) x = 2 x *= 3 >> 6
import operator x = [Link](2, 3)

/= [Link](a,b) x = 6 x /= 3 >> 2
import operator x = [Link](6, 3)

44
Python Operators >> Assignment Operators

45

You might also like