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

Field of Technological Industries: Computer Science (ETP211) : (Lec)

The document provides an introduction to Python programming, focusing on the input() function, arithmetic operators, and various built-in functions. It includes examples of programs for user input, age calculation, area calculations, and operator precedence. Additionally, it explains unary and binary operators, compound assignment operators, and demonstrates the use of expressions in Python.

Uploaded by

alrashidy015
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)
7 views20 pages

Field of Technological Industries: Computer Science (ETP211) : (Lec)

The document provides an introduction to Python programming, focusing on the input() function, arithmetic operators, and various built-in functions. It includes examples of programs for user input, age calculation, area calculations, and operator precedence. Additionally, it explains unary and binary operators, compound assignment operators, and demonstrates the use of expressions in Python.

Uploaded by

alrashidy015
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
You are on page 1/ 20

Field of Technological Industries

For All Programs

Computer Science (ETP211): (Lec6)

Dr : Wael Mohamed Fawaz Date : 4 / 11 / 2024


Basics of Python Programming
The input() function
It is used to accept an input from a user.
Syntax:
Variable_Name = input()
OR
Variable_Name = input(‘String’)

Example:
>>> str1 = input('Please Enter the String:')
Please Enter the String:amit
>>> print(str1)
amit
Introduction to Computer and Python Programming
The input() function
The input() function is used to accept an input from a user. A programmer can ask a user to
input a value by making use of input().

PROGRAM: Write a program to read strings from the keyboard.


Str1 = input(‘Type your name: ’)
Str2 = input(‘Type your ID: ’)
print(‘Hello‘,Str1)
print(‘Your Id = ‘,Str2)

Output
Type your name: Ahmed
Type your ID: 221100218
Hello Ahmed
Your Id = 221100218
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate your age

N = input(‘Enter your year of birth: ’)


N = int(N) #int() Changes the string to integer
age = 2024 - N
print(‘You age is ‘,age, ‘years old’)

Output
Enter your year of birth: 2002
You age is 22 years old
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate the area of a rectangle
L1=int(input(‘The length= ’))
L2=int(input(‘The width= ’))
a=L1*L2
print(‘Length=’, L1, ‘width= ’, L2, ‘area= ‘, a)
Output
Length = 2, width = 5, area = 10
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate the area of a circle.
L=int(input(‘The radius= ’))
a=3.14*L**2
print(‘radius=’, L, ‘area= ‘, a)
Output
radius = 10, area = 314
Introduction to Computer and Python Programming
PROGRAM: Write a program to print name, age and hight for a person using input function.

N = input(‘Type your name: ’)


A = int(input(‘Age = ‘)) #int() Changes the string to integer
H = float(input(‘Hight = ’)) #float() Changes the string to float
print(N, ‘is ’, A, ‘years old. ’)
print(‘His height is ‘, H, ‘cm.’)

Output
Ahmed is 20 years old.
His height is 185.5 cm.
Introduction to Expression and Operators
An expression in Python is a block of code that produces a result or
value upon evaluation.

An expression can be broken down into operators and operands.

Operators are symbols help the user or command computer to perform


mathematical operations.

Example:
In the expression 6 + 3 the ‘+’ act as operator. Where operator
requires operands to perform operations. Therefore 6 and 3 act as
operands.
Arithmetic Operators
There are two types of Arithmetic Operators
a) Unary Operator b) Binary Operator
Unary Operators:
Unary arithmetic operators perform mathematical operations on one operand only. The ‘+’ and ‘-’ are two
unary operators.
Example:
>>> x = -5 #Negates the value of X
>>> x
-5
Binary Operators
Binary operators are operators which require two operands .
Operator is written in between two operands.
Example:
>>> 3 + 10
>>>13
>>> 10 – 7
>>> 3
Arithmetic Operators
Division Operator
If the division operator is applied in between two operands, it returns the
result as the arithmetic quotient of the operands.

Example:
>>> 4/2
>>> 2.0
>>> 2/3
0.6666666666666666
Arithmetic Operators
Modulo (%) Operator
When the second number divides the first
number, the modulo operator returns the
remainder.

Example:
>>> 21 % 9
3
Arithmetic Operators
The Exponent ** Operator
The ‘**’ exponent operator is used to calculate the power or exponent
of a number

To compute xY (X raised to Y), the expression is written as X**Y.

Example
>>> 5**4
625
Operator Precedence and Associativity
• The operator precedence determines the order in which the python interpreter evaluates the
operators in an expression.
• Where as associativity property decides which operation to be performed first.
• Associativity is of two types.
a) Left to Right
Example:
4+6–3+2 = 9
b) Right to Left
X = Y = Z = Value
Note:
When the operators of same priority are found in the expression, precedence is given to the
left most operator.
Operator Precedence and Associativity
Example:
Z=4*6+8/2
= 28
In the above expression * is evaluated first, even though * and / have the
same priorities. The operator * occurs before / and hence the evaluation
starts from left to right.
Precedence Operators Associativity
Highest () Innermost to Outermost

** Highest
* , / , // , % Left to Right
+- Left to Right

Lowest
Compound Assignment Operator
• The operators +, *, //, /, % and ** are used with assignment operator (=) to
form compound or augmented assignment operator.

Example:
X=X+1
But python allows programmer to combine assignment and addition operator.
Thus the above statement X = X + 1 can also be written as
X+=1
Various other Compound Assignment operators are as follows
*= , /= //=, %=, **=, -= , and !=
Some Examples

PROGRAM 3.8 Write a program to


display the following table.
print(‘X \t Y \t X**Y’)
X Y X**Y print(‘10 \t 2 \t ‘,10**2)
10 2 100 print(‘10 \t 3 \t ‘,10**3)
10 3 1000 print(‘10 \t 4 \t ‘,10**4)
10 4 10000 print(‘10 \t 5 \t ‘,10**5)
10 5 100000 Output
X Y X**Y
10 2 100
10 3 1000
10 4 10000
10 5 100000
Some Examples

Example
>>> z=(5+6)*10
>>> z
110

Explanation
In the above example, z is initialized with one expression (5+6)*10. The
sub expression (5 + 6) is
evaluated first, followed by the multiplication operation.
Some Examples

Some More Complex Examples


>>> A= 100 / (2*5)
>>> A
10.0

>>> B= 4 + (5 * (4/2) + (4 + 3))


>>> B
21.0
Basics of Python Programming
Python Various inbuilt function
Name of Function Meaning Example:
max(x1,x2, x3,…….XN) Returns Largest among >>>max(10,20,30)
X1, X2,…..XN >>>30

min(x1,x2, x3,…….XN) Returns smallest among >>>min(10,20,30)


X1, X2,…..XN >>>30

abs(x) Returns absolute value of >>>abs(-10)


x >>>10
>>>abs(4)
4
pow(X,Y) Returns XY >>>pow(2,3)
8
round(x) Return the value nearest to >>> round(10.23)
the value of x 10
>>> round(10.90)
11

You might also like