Basic Python B2B
Basic Python B2B
Python Programming
● Lists, Dictionaries
● Conditional Statements
● Looping Constructs
● Dynamic semantics
What is Semantics?
A set of rules determining if a certain phrase makes sense
Example : I ate an apple
Apple ate me
for i in range(1,11):
print(i)
if i == 5:
break
● Multi-line comments
Use triple quotes, either ''' or ""“
"""This is a
perfect example of
multi-line comments"""
● Colab is a Python development environment that runs in the browser using Google Cloud
● Colab uses Python 3.6. 9 which should run everything without any errors
● The declaration happens automatically when you assign a value to a variable. The equal sign (=)
is used to assign values to variables.
● The operand to the left of the = operator is the name of the variable and the operand to the right
of the = operator is the value stored in the variable.
● Assignment Operators
● Bitwise Operators
● Logical Operators
a = [5,10,15,20,25,30,35,40]
# a[2] = 15
print("a[2] = ", a[2])•
# t[1] = 'program'
print("t[1] = ", t[1])
● It is generally used when we have a huge amount of data. Dictionaries are optimized for
retrieving data. We must know the key to retrieve the value.
● •In Python, dictionaries are defined within braces {} with each item being a pair in the form
key:value. Key and value can be of any type.
d = {1:'value','key':2}
s = "This is a string"
s = '''A multiline
string''‘
a = {5,2,3,1,4}
print("a = ", a)
● The end and sep parameters can be used for formatting the output of the print() function.
● Example :
#For formatting a date 28-07-2021
print(‘28',‘07','2021', sep='-')
#For email roshan@whitehatjr
print(‘roshan',‘whitehatjr', sep='@')
● Decision making statements in programming languages decides the direction of flow of program
execution. Decision making statements available in python are:
● If statement
● If..else statements
● nested if statements
● if-elif-else ladder
● Example:
i = 10
if (i > 15):
print (“I am inside If Block")
print ("I am Not in if Block")
● .Syntax:
i = 20;
if (i < 15):
print ("i is smaller than 15")
else:
print ("i is greater than 15")
print ("i'm not in if nor in else Block")
● .Syntax:
if (condition): # Executes this block if condition is true
● .Example:
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
● Loops
While loop and For loop
● Example
count = 0
while (count < 3):
count = count + 1
print("Hello Teachers")
● Example
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)
● continue statement is opposite to that of break statement, instead of terminating the loop, it
forces to execute the next iteration of the loop.
● The pass statement is a null statement. But the difference between pass and comment is that
comment is ignored by the interpreter whereas pass is not ignored.
● Functions help break our program into smaller and modular chunks. As our program grows larger
and larger, functions make it more organized and manageable..
● The pass statement is a null statement. But the difference between pass and comment is that
comment is ignored by the interpreter whereas pass is not ignored.
● Syntax
def function_name(parameters):
statement(s)
● Example
def greet(name):
print("Hello, " + name + ". Good morning!")
© 2020 WhiteHat Education Technology Private Limited. All rights reserved
Lambda function in Python
● A lambda function is a small anonymous function.
● A lambda function can take any number of arguments, but can only have one expression.
● The power of lambda is better shown when you use them as an anonymous function inside
another function.
● Syntax
lambda arguments : expression
● Example
x = lambda a : a + 10
print(x(5))
● Example:
○ In real life, a car is an object. The car has properties, such as weight and colour, and function,
such as drive and brake.In the real world, for example, there may be thousands of cars in
existence,all of the same make and model. Each car was built from the same set of blueprints
and therefore contains the same components. In object-oriented terms, we say that your car is
an instance (object) of the class Car.