KEMBAR78
Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx
Introduction to
Programming
Languages
Welcome Python Programming 1
Programming language
•A programming language is a system of
notation for writing computer programs
• Programming is used to automate, maintain, assemble,
measure, and interpret the processing of the data and
information
Welcome Python Programming 2
Programs
•Programs are written in programming languages
•Pieces of the same program can be written in
different programming languages
•A set of rules and symbols used to construct a
computer program
•A language used to interact with the computer
A Quick
Tour of
Python
4
The Programming Cycle for
Python
Jan-24 Programming 5
Write/Edit
Run
with some input
OK?
YES
More
Inputs?
YES
NO
NO
Jan-24 Programming 6
Jan-24 Programming 7
Filename, preferred extension is py
User Program
Python Shell is Interactive
Jan-24 Programming 8
IN[1]:
IN[2]:
IN[4]:
IN[3]:
Python Shell Prompt
User Commands
(Statements)
Outputs
( )
Elements of Python
• A Python program is a sequence of definitions and commands
(statements)
• Commands manipulate objects
• Each object is associated with a Type
• Type:
• A set of values
• A set of operations on these values
• Expressions: An operation (combination of objects and operators)
Jan-24 Programming 9
Types in Python
• int
• Bounded integers, e.g. 732 or -5
• float
• Real numbers, e.g. 3.14 or 2.0
• long
• Long integers with unlimited precision
• str
• Strings, e.g. ‘hello’ or ‘C’
• bool
• Boolean, true or false
Jan-24 Programming 10
Example of Types
Jan-24 Programming 11
Type Conversion (Type Cast)
• Conversion of value of one type to other
• We are used to int ↔ float conversion in Math
• Integer 3 is treated as float 3.0 when a real number is expected
• Float 3.6 is truncated as 3, or rounded off as 4 for integer contexts
• Type names are used as type converter functions
Jan-24 Programming 12
Type Conversion Examples
Jan-24 Programming 13
Note that float to int conversion
is truncation, not rounding off
Type Conversion and Input
Jan-24 Programming 14
Operators
• Arithmetic
• Comparison
Jan-24 Programming 15
+ - * // / % **
== != > < >= <=
Variables
• A name associated with an object
• Assignment used for binding
m = 64;
c = ‘Acads’;
f = 3.1416;
• Variables can change their bindings
f = 2.7183;
Jan-24 Programming 16
64
Acads
3.1416
2.7183
m
c
f
Assignment Statement
• A simple assignment statement
Variable = Expression;
• Computes the value (object) of the expression on the right hand side
expression (RHS)
• Associates the name (variable) on the left hand side (LHS) with the
RHS value
• = is known as the assignment operator.
Jan-24 Programming 17
Programming using Python
Operators and Expressions
1/22/2024 Programming 18
Binary Operations
1/22/2024 Programming 19
Op Meaning Example Remarks
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.50 In Python3
9.1/2.0 is 4.55 Real div.
// Integer Division 9//2 is 4
% Remainder 9%2 is 1
The // operator
• Also referred to as “integer division”
• Result is a whole integer (floor of real division)
• But the type need not be int
• the integral part of the real division
• rounded towards minus infinity (−∞)
• Examples
1/22/2024 Programming 20
9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0
1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
The % operator
•The remainder operator % returns the
remainder of the result of dividing its
first operand by its second.
1/22/2024 Programming 21
9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0
9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4
Ideally: x == (x//y)*y + x %y
Conditional Statements
•In daily routine
•If it is very hot, I will skip exercise.
•If there is a quiz tomorrow, I will
first study and then sleep.
Otherwise I will sleep now.
•If I have to buy coffee, I will
go left. Else I will go
straight.
Jan-24 Programming 22
if-else statement
• Compare two integers and print the min.
Jan-24 Programming 23
1. Check if x is less
than y.
2. If so, print x
3. Otherwise, print y.
if x < y:
print (x)
else:
print (y)
print (‘is the minimum’)
x,y = 6,10
if x < y:
print (x)
else:
print (y)
print (‘is the min’)
x y
6 10
Run the program
Output
6
Indentation
•Indentation is important in Python
• grouping of statement (block of statements)
• no explicit brackets, e.g. { }, to group statements
Jan-24 Programming 24
if statement (no else!)
• General form of the if statement
• Execution of if statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and then control moves to
the S2.
• If expression evaluates to false, then control moves to the S2 directly.
Jan-24 Programming 25
if boolean-expr :
S1
S2
S1
S2
if-else statement
• General form of the if-else statement
• Execution of if-else statement
• First the expression is evaluated.
• If it evaluates to a true value, then S1 is executed and
then control moves to S3.
• If expression evaluates to false, then S2 is executed and
then control moves to S3.
• S1/S2 can be blocks of statements!
Jan-24 Programming 26
if boolean-expr :
S1
else:
S2
S3
S2
S1
S3
Nested if, if-else
Jan-24 Programming 27
if a <= b:
if a <= c:
…
else:
…
else:
if b <= c) :
…
else:
…
Elif
• A special kind of nesting is the chain of if-else-if-else-… statements
• Can be written elegantly using if-elif-..-else
Jan-24 Programming 28
if cond1:
s1
elif cond2:
s2
elif cond3:
s3
elif …
else
last-block-of-stmt
if cond1:
s1
else:
if cond2:
s2
else:
if cond3:
s3
else:
…
Summary of if, if-else
•if-else, nested if's, elif.
•Multiple ways to solve a problem
•issues of readability,
maintainability
•and efficiency
Jan-24 Programming 29
Class Quiz
• What is the value of expression:
a) Run time crash/error
b) I don’t know / I don’t care
c) False
d) True
Jan-24 Programming 30
(5<2) and (3/0 > 1)
The correct answer is
False
Programming using Python
Loops
Jan-24 Python Programming 31
Program…
Jan-24 Python Programming 32
n = int(input('Enter a number: '))
print (n, 'X', 1, '=', n*1)
print (n, 'X', 2, '=', n*2)
print (n, 'X', 3, '=', n*3)
print (n, 'X', 4, '=', n*4)
print (n, 'X', 5, '=', n*5)
print (n, 'X', 6, '=', n*6)
….
Too much
repetition!
Can I avoid
it?
While Statement
1. Evaluate expression
2. If TRUE then
a) execute statement1
b) goto step 1.
3. If FALSE then execute statement2.
Jan-24 Python Programming 33
while (expression):
S1
S2
FALSE
TRUE
S1
expression
S2
For loop in Python
• General form
Jan-24 Python Programming 34
for variable in sequence:
stmt
For Loop
Jan-24 Python Programming 35
The range() Function: To loop through a set
of code a specified number of times, we
can use the range() function
 for x in range(6):
print(x)
For Loop
Jan-24 Python Programming 36
The range() function defaults to
0 as a starting value, it is
possible to specify the starting
value by adding a parameter:
range(2, 6), which means values
from 2 to 6 (but not including
6):
 for x in range(2, 6):
print(x)
# print all odd numbers < 10
i = 1
while i < 10:
print(i)
i += 2
Quiz
Jan-24 Python Programming 37
Welcome Python Programming 38

Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx

  • 1.
  • 2.
    Programming language •A programminglanguage is a system of notation for writing computer programs • Programming is used to automate, maintain, assemble, measure, and interpret the processing of the data and information Welcome Python Programming 2
  • 3.
    Programs •Programs are writtenin programming languages •Pieces of the same program can be written in different programming languages •A set of rules and symbols used to construct a computer program •A language used to interact with the computer
  • 4.
  • 5.
    The Programming Cyclefor Python Jan-24 Programming 5
  • 6.
  • 7.
    Jan-24 Programming 7 Filename,preferred extension is py User Program
  • 8.
    Python Shell isInteractive Jan-24 Programming 8 IN[1]: IN[2]: IN[4]: IN[3]: Python Shell Prompt User Commands (Statements) Outputs ( )
  • 9.
    Elements of Python •A Python program is a sequence of definitions and commands (statements) • Commands manipulate objects • Each object is associated with a Type • Type: • A set of values • A set of operations on these values • Expressions: An operation (combination of objects and operators) Jan-24 Programming 9
  • 10.
    Types in Python •int • Bounded integers, e.g. 732 or -5 • float • Real numbers, e.g. 3.14 or 2.0 • long • Long integers with unlimited precision • str • Strings, e.g. ‘hello’ or ‘C’ • bool • Boolean, true or false Jan-24 Programming 10
  • 11.
  • 12.
    Type Conversion (TypeCast) • Conversion of value of one type to other • We are used to int ↔ float conversion in Math • Integer 3 is treated as float 3.0 when a real number is expected • Float 3.6 is truncated as 3, or rounded off as 4 for integer contexts • Type names are used as type converter functions Jan-24 Programming 12
  • 13.
    Type Conversion Examples Jan-24Programming 13 Note that float to int conversion is truncation, not rounding off
  • 14.
    Type Conversion andInput Jan-24 Programming 14
  • 15.
    Operators • Arithmetic • Comparison Jan-24Programming 15 + - * // / % ** == != > < >= <=
  • 16.
    Variables • A nameassociated with an object • Assignment used for binding m = 64; c = ‘Acads’; f = 3.1416; • Variables can change their bindings f = 2.7183; Jan-24 Programming 16 64 Acads 3.1416 2.7183 m c f
  • 17.
    Assignment Statement • Asimple assignment statement Variable = Expression; • Computes the value (object) of the expression on the right hand side expression (RHS) • Associates the name (variable) on the left hand side (LHS) with the RHS value • = is known as the assignment operator. Jan-24 Programming 17
  • 18.
    Programming using Python Operatorsand Expressions 1/22/2024 Programming 18
  • 19.
    Binary Operations 1/22/2024 Programming19 Op Meaning Example Remarks + Addition 9+2 is 11 9.1+2.0 is 11.1 - Subtraction 9-2 is 7 9.1-2.0 is 7.1 * Multiplication 9*2 is 18 9.1*2.0 is 18.2 / Division 9/2 is 4.50 In Python3 9.1/2.0 is 4.55 Real div. // Integer Division 9//2 is 4 % Remainder 9%2 is 1
  • 20.
    The // operator •Also referred to as “integer division” • Result is a whole integer (floor of real division) • But the type need not be int • the integral part of the real division • rounded towards minus infinity (−∞) • Examples 1/22/2024 Programming 20 9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0 1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
  • 21.
    The % operator •Theremainder operator % returns the remainder of the result of dividing its first operand by its second. 1/22/2024 Programming 21 9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0 9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4 Ideally: x == (x//y)*y + x %y
  • 22.
    Conditional Statements •In dailyroutine •If it is very hot, I will skip exercise. •If there is a quiz tomorrow, I will first study and then sleep. Otherwise I will sleep now. •If I have to buy coffee, I will go left. Else I will go straight. Jan-24 Programming 22
  • 23.
    if-else statement • Comparetwo integers and print the min. Jan-24 Programming 23 1. Check if x is less than y. 2. If so, print x 3. Otherwise, print y. if x < y: print (x) else: print (y) print (‘is the minimum’)
  • 24.
    x,y = 6,10 ifx < y: print (x) else: print (y) print (‘is the min’) x y 6 10 Run the program Output 6 Indentation •Indentation is important in Python • grouping of statement (block of statements) • no explicit brackets, e.g. { }, to group statements Jan-24 Programming 24
  • 25.
    if statement (noelse!) • General form of the if statement • Execution of if statement • First the expression is evaluated. • If it evaluates to a true value, then S1 is executed and then control moves to the S2. • If expression evaluates to false, then control moves to the S2 directly. Jan-24 Programming 25 if boolean-expr : S1 S2 S1 S2
  • 26.
    if-else statement • Generalform of the if-else statement • Execution of if-else statement • First the expression is evaluated. • If it evaluates to a true value, then S1 is executed and then control moves to S3. • If expression evaluates to false, then S2 is executed and then control moves to S3. • S1/S2 can be blocks of statements! Jan-24 Programming 26 if boolean-expr : S1 else: S2 S3 S2 S1 S3
  • 27.
    Nested if, if-else Jan-24Programming 27 if a <= b: if a <= c: … else: … else: if b <= c) : … else: …
  • 28.
    Elif • A specialkind of nesting is the chain of if-else-if-else-… statements • Can be written elegantly using if-elif-..-else Jan-24 Programming 28 if cond1: s1 elif cond2: s2 elif cond3: s3 elif … else last-block-of-stmt if cond1: s1 else: if cond2: s2 else: if cond3: s3 else: …
  • 29.
    Summary of if,if-else •if-else, nested if's, elif. •Multiple ways to solve a problem •issues of readability, maintainability •and efficiency Jan-24 Programming 29
  • 30.
    Class Quiz • Whatis the value of expression: a) Run time crash/error b) I don’t know / I don’t care c) False d) True Jan-24 Programming 30 (5<2) and (3/0 > 1) The correct answer is False
  • 31.
  • 32.
    Program… Jan-24 Python Programming32 n = int(input('Enter a number: ')) print (n, 'X', 1, '=', n*1) print (n, 'X', 2, '=', n*2) print (n, 'X', 3, '=', n*3) print (n, 'X', 4, '=', n*4) print (n, 'X', 5, '=', n*5) print (n, 'X', 6, '=', n*6) …. Too much repetition! Can I avoid it?
  • 33.
    While Statement 1. Evaluateexpression 2. If TRUE then a) execute statement1 b) goto step 1. 3. If FALSE then execute statement2. Jan-24 Python Programming 33 while (expression): S1 S2 FALSE TRUE S1 expression S2
  • 34.
    For loop inPython • General form Jan-24 Python Programming 34 for variable in sequence: stmt
  • 35.
    For Loop Jan-24 PythonProgramming 35 The range() Function: To loop through a set of code a specified number of times, we can use the range() function  for x in range(6): print(x)
  • 36.
    For Loop Jan-24 PythonProgramming 36 The range() function defaults to 0 as a starting value, it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):  for x in range(2, 6): print(x)
  • 37.
    # print allodd numbers < 10 i = 1 while i < 10: print(i) i += 2 Quiz Jan-24 Python Programming 37
  • 38.