lec 18,19
lec 18,19
https://www.python.org/downloads/
https://www.w3schools.com/python/
FIRST PYTHON CODE
List of Operators :
i. Add : + (eg. 2+3=5)
ii. Subtract : - (eg. 5-3=2)
iii. Multiply : * (eg. 5*3=15)
iv. Divide : / (floating-point division, eg. 10/2=5.0) , //
(integer division, eg. 10//2 = 5)
v. Exponential : ** (eg. 2**3 = 8)
vi. Modulus : % (eg. 5%2 =1) (gets the remainder)
DATA TYPES
• Text Type : str
• Numeric Types : int, float, complex
• Sequence Types : list, tuple, range
• Mapping Type : dict
• Set Types : set, frozenset
• Boolean Type : bool
• Binary Types : bytes, bytearray, memoryview
VARIABLES
Variable is a small space allocated in memory for storing
some value (including NULL’s or no value)
Variable names :
i. can be as long as you like
ii. must start with a letter or underscore (‘_’)
iii. can contain any combination of letters, number and
underscore
iv. cannot contain reserved words (keywords)
v. are case-sensitive
Eg. _aprateem, aprateem_roy, _aroy53____ (correct)
+sgfafgbef , 42dfgef_avav , for (incorrect)
PYTHON KEYWORDS
False ,class ,finally, is, return
None, continue, for, lambda, try
True, def, from, nonlocal, while
and, del, global, not, with
as, elif, if, or, yield
assert, else, import, pass
break, except, in, raise
EXERCISE
Which of the following is/are correct variable name(s)?
a) +bapt456
b) _bapita456
c) 456bap_
d) Bp_53_ROY
e) class
STATEMENTS & EXPRESSIONS
Statements are a unit of code that primarily :
✓ Allocate some value to a variable (Assignment)
(eg. a=5 , b=‘This is Python !’ , c = 15.345)
✓ Display some output
(eg. print(a) , print(a+c), print(b,c))
Expressions are :
✓ combination of variables, values and operators
✓ can be a variable or a literal(i.e. constant, eg. 5, ‘Hi’) by
itself (eg. print(‘This is Python !’), print(5))
ORDER OF MATHEMATICAL
OPERATIONS
P = Parentheses
E = Exponentiation
M = Multiplication
D = Division
A = Addition
S = Subtraction
✓ Repetition of strings : *
Eg. print(‘Hi’*2)
HiHi
IMPORTANT POINTS
• A+B = ?
• B/A = ?
• B//A = ?
• C+A = ?
• C*3 = ?
• A*2+(B-5) = ?
• A+C-B = ?
SCRIPT
✓ A program file containing statements or lines of code in a certain
order
✓ To execute a script , please save the lines of code into a file (eg.
sum.py) and type the following in the command prompt :
NOTE :
❖ every python script must have an extension of ‘.py’
❖ the ‘python’ keyword tells the OS to execute a python script
FIRST PYTHON SCRIPT
✓ Write a script that prints your name in the following manner :
My name is <yourname>
Eg. My name is bapita
✓ Save it as : <yourname>.py
(eg. My script name : bapita.py)
def display_name(my_name) :
statement .. Lines of code
statement ..
return <value>
a=‘Bapita’
display_name(a)
python function.py
CONDITIONAL EXECUTION
(if STATEMENT)
✓ Based on a Boolean expression (that evaluates to either
True or False) certain statements may or may not get
executed
3. Write a script that prints your name if you are over 18 years of
age. If you are not over 18, it checks the following :
if age less than 8 => ‘Toddler’
If age between 9 and 12 => ‘Infant’
If age between 13 and 17 => ‘Adolescent’
GETTING INPUTS FROM USER
Eg. :
name=input()
age=input(‘Enter your age please - ’)
last_name=input(‘Enter your age please - \n’)
OR
➢ till a boolean expression becomes False (while loop)
while (bool exp) :
execute statement(s)
INFINITE LOOPS
➢ Which run forever until the system crashes because it
runs out of memory for processing further data
➢ Should be avoided at all costs by carefully constructing
boolean expressions
➢ Can be stopped using break command
eg. i=1
while True:
print(i)
i=i+1
if i >10 : break
➢ For skipping iterations , use continue command
WORKING WITH STRINGS
➢ A sequence of characters
➢ Access any character by its position in the string(called
index)
eg. fruit = ‘banana’
print(fruit[1])
Output : ‘a’ (WHY ?)