Python Unit 1
Python Unit 1
Write/Edit
OK?
NO
YES
YES
NO More
Inputs?
2 Programming 1/7/23
Basics
⚫ Type into the interactive shell, also called the
REPL (Read-Evaluate-Print Loop), which lets
you run (or execute)
Basics
⚫ Python instructions one at a time and
instantly shows you the results.
⚫ Expression
⚫ Values
⚫ operators
Basics
⚫ Error
1. (A+B)*(C-D)
2. A//C%3
3. D*(B+A)
4 . C + (B - C)/D
S y n t a x Error
Questions
⚫ Which of the following are operators, and
which are values?
X
A s s i g n m e n t Statements
⚫ An assignment statement consists of a
variable name, an equal sign (called the
assignment operator), and the value to be
stored.
⚫ Eg spam = 42,
A s s i g n m e n t Statements
variable initialization
Variable N a m e s
⚫ A good variable name describes the data it contains
⚫ Example?
⚫ Naming restrictions
1. It can be only one word with no spaces.
2. It can use only letters, numbers, and the underscore (_)
character.
3. It can’t begin with a number.
4. Variable names are case-sensitive, meaning that spam,
SPAM, Spam, and sPaM are four different variables.
5. Python convention to start your variables with a
lowercase letter.
Variable N a m e s
Comments
Comments
⚫ print( )?
The input() Function
Blocks of Code
There are three rules for blocks.
•Blocks begin when the indentation
increases.
• Blocks can contain other blocks.
• Blocks end when the indentation
Flow Control S t a t e m e n t s
if Statements
• The if keyword
•A condition that is, an expression that
evaluates to True or False
• A colon
•Starting on the next line, an indented block
of code (called the if clause)
Flow Control S t a t e m e n t s
Flow Control S t a t e m e n t s
else S t at em ents
“If this condition is true, execute this
code. Or else, execute that code.”
An else statement doesn’t have a
condition, consists of the following:
• The else keyword
• A colon
•Starting on the next line, an indented
block of code (called the else clause)
PLC142
Conditional Statements
Flow Control S t a t e m e n t s
else S t at em ents
Flow Control S t a t e m e n t s
PLC142
Conditional Statements
Flow Control S t a t e m e n t s
elif Statements
you may have a case where you
want one of many possible clauses to
execute.
elif statement always consists of the
following:
• The elif keyword
•A condition (that is, an expression that
evaluates to True or False)
• A colon
•Starting on the next line, an indented
block of code (called the elif)
PLC142
Conditional Statements – elif
Flow Control S t a t e m e n t s
PLC142
PLC142
while L o o p S t a t e m e n t s
You can make a block of code execute over and
over again using a while statement.
• The while keyword
• A condition
• A colon
•Starting on the next line, an indented block of
code (called the while clause)
At the end of a while clause, the program
execution jumps back to the start of the while
statement.
The while clause is often called the while loop or
just the loop.
while L o o p S t a t e m e n t s
Composition
break Statements
If the execution reaches a break statement,
it immediately exits the while loop’s clause.
continue Statements
When the program execution reaches a
continue statement, the program execution
immediately jumps back to the start of the
loop and revaluates the loop’s condition.
List of Program using while loop
Syntax
range(start, stop, step)
For example,
from random import *.
E n d i n g a P r o g r a m Early wit h t he s ys .exit( ) Function
a=a+5 a+=5
b =b*2 b*=2
c = c/5 c/ = 5
d = d -6 d-= 6
Functions
• Easy
• Reuse
• Built-in functions
>>> round(3.8)
4
>>> round(3.3)
3
>>> round(3.5)
4
>>> round(-3.3)
-3
>>> round(-3.5)
-4
>>> round( 3.141592653 , 2)
3.14
Functions
Memory Addresses: How Python
Keeps Track of Values
⚫ Python keeps track of each value in a separate
object and that each object has a memory
address.
⚫ You can discover the actual memory address
of an object using built-in function id:
Memory Addresses
Function also have Memory Addresses
Defin in g O u r O w n
Functions
⚫ The general form of a function definition is
as follows:
⚫ Example
Defining Our O w n Functions
return «e x p r e s s io n»
Using Local Variables for Temporary
Storage
variable’s scope. The scope of a local
variable is from the line in which it is
defined up until the end of the function.
K eywords