Python basics
Python basics
Akshata S. Bhayyar
Assistant Professor, Dept. of CSE, MSRIT
Run
with some input
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
Syntax Error
Questions
Which of the following are operators, and
which are values?
X
Assignment 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,
Assignment Statements
variable initialization
Variable Names
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 Names
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 Statements
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 Statements
Flow Control Statements
else Statements
“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)
Flow Control Statements
else Statements
Flow Control Statements
Flow Control Statements
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
Flow Control Statements
while Loop Statements
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 Loop Statements
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 *.
Ending a Program Early with the sys.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
Defining Our Own
Functions
The general form of a function definition is
as follows:
Example
Defining Our Own Functions
return «expression»
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.
Keywords