First Level
(First Semester)
___________________________________________
Python Programming
___________________________________________
Lecture 2
Dr. Amira I. Abdelatey
1
Assign value to variable
• A variable must be assigned a value before
it can be used in an expression.
2
Simultaneous Assignments
• Swap value of x and y
• Another way
3
Read input from the console
• Reading input from the console enables the
program to accept input from the user.
• variable = input("Enter a value: ")
4
Compute area with input
5
Compute average
6
Compute average with
simultaneous assignment
7
Variables (identifiers)
• Variable: the names that identify the
elements
– raduis = 1
– pi = 3.14
– area=pi*r*r
– count=3
• Variables contains only digits,characters,_
• Variables must start only with character
8
Numeric Data Types and
Operators
• Python has two numeric types—integers
and floating-point numbers—for working
with the operators +, -, *, /, //, **, and %.
9
The / Operator
• The / operator performs a float division that
results in a floating number. For example,
10
The // operator
• The // operator performs an integer division;
the result is an integer, and any fractional
part is truncated. For example,
11
The % Operator
• The % operator, known as remainder or
modulo operator, yields the remainder after
division.
• 9%2 = 1
12
Using // and % operator
13
Expression evaluation
14
Addition assignment operator
• count = count + 1
• Python allows you to combine assignment
and addition operators using an augmented
(or compound) assignment operator. For
instance, the preceding statement can be
written as:
• count += 1
• The += operator is called the addition
assignment operator. 15
Augmented assignment operator
16
Type Conversions
• If one of the operands for the numeric
operators is a float value, the result will be
a float value.
17
Rounding
• Use the round function to round a number
to the nearest whole value
18
Sales Tax
19
What are the results of the following expressions?
Expression Result
• 42 / 5 _____
• 42 // 5 _____
• 42 % 5 _____
• 40 % 5 _____
• 1%2 _____
• 2%1 _____
• 45 + 4 * 4 - 2 _____
• 45 + 43 % 5 * (23 * 3 % 2) _____
• 5 ** 2 _____
• 5.1 ** 2 _____
20