Basic Python B2B

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Introduction to

Python Programming

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Agenda
● Introduction

● Variables and print statement, Numbers, Strings

● Operators and their types

● Lists, Dictionaries

● Tuples, Mutable vs Immutable

● Conditional Statements

● Looping Constructs

● Function, Lambda Functions

● Object Oriented Programming in a nutshell

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


What is Python?
● Interpreted, object-oriented, high-level programming language

● Dynamic semantics

● Simple, easy to learn syntax emphasizing readability

● Python supports modules and packages

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


History of Python Programming
● Invented in the Netherlands, early 90s by Guido van Rossum Python
was conceived in the late 1980s and its implementation was started
in December 1989

● Guido Van Rossum is fan of ‘Monty Python’s Flying Circus’, this is


a famous TV show in Netherlands

● Named after Monty Python

● Open sourced from the beginning

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Versions
● Python 3.9.2 documentation released on 19 Feb 2021
● For more information visit www.python.org

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Compiler vs Interpreter

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Applications of Python

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Why Python?

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Language
What is Syntax?
A set of rules used to determine if a certain string of words forms a valid sentence.
Example: I like playing cricket
I cricket like playing

What is Semantics?
A set of rules determining if a certain phrase makes sense
Example : I ate an apple
Apple ate me

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python
● Case sensitive. False await else import pass

● 33 keywords in Python 3.7. None break except in raise

● Number of keywords can vary slightly True class finally is return


over the course of time.
and continue for lambda try

as def from nonlocal while

assert del global not with

async elif if or yield

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python Indentation
● Here is an example:

for i in range(1,11):
print(i)
if i == 5:
break

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Comments
● Python Comments
#This is a comment

● Multi-line comments
Use triple quotes, either ''' or ""“
"""This is a
perfect example of
multi-line comments"""

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


IDE
● An integrated development environment (IDE) is software for building applications that
combines common developer tools into a single graphical user interface (GUI).

● An IDE can significantly speed up your work.

● Example: PyCharm, Spyder, Thonny, Google Colab

● 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

● •Refer following link for more info on IDE: https://realpython.com/python-ides-code-editors-guide/

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Variables and Data types
● Variables are nothing but reserved memory locations to store values.

● Based on the data type of a variable, the interpreter allocates memory

● The data stored in memory can be of many types.

● Python has five standard data types −


• Numbers
• String
• List
• Tuple
• Dictionary

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Assigning Values
● Python variables do not need explicit declaration to reserve memory space.

● 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.

● Assigning multiple values to multiple variables


a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Operators in Python
Python language supports the following types of operators.
● Arithmetic Operators

● Comparison (Relational) Operators

● Assignment Operators

● Bitwise Operators

● Logical Operators

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Operators in Python (contd)...

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Operators in Python (contd)...

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Operators in Python (contd)...

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python List
● List is an ordered sequence of items. It is one of the most used data type in Python and is very flexible. All the
items in a list do not need to be of the same type.
● Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

a = [1, 2.2, 'python']

a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])•

# a[0:3] = [5, 10, 15]


print("a[0:3] = ", a[0:3])

# a[5:] = [30, 35, 40]


print("a[5:] = ", a[5:])
© 2020 WhiteHat Education Technology Private Limited. All rights reserved
Python List - Built in Methods

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python Tuple
● Tuple is an ordered sequence of items same as a list. The only difference is that tuples are
immutable. Tuples once created cannot be modified.
● Tuples are used to write-protect data and are usually faster than lists as they cannot change
dynamically.
● It is defined within parentheses ()
where items are separated by commas.
t = (5,'program', 1+3i)

# t[1] = 'program'
print("t[1] = ", t[1])

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python Dictionary
● Dictionary is an unordered collection of key-value pairs.

● 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}

print("d[1] = ", d[1]);


#d[1] = value

print("d['key'] = ", d['key']);


#d['key'] = 2
© 2020 WhiteHat Education Technology Private Limited. All rights reserved
Python Dictionary (contd)...

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python Strings
● String is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """.

● Strings are immutable

s = "This is a string"
s = '''A multiline
string''‘

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Python Set
● Set is an unordered collection of unique items. Set is defined by values separated by comma
inside braces { }. Items in a set are not ordered.

a = {5,2,3,1,4}
print("a = ", a)

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Print Function
● Python provides numerous built-in functions that are readily available to us at the Python prompt.

● •Python 3.7.1 comes with 69 built-in functions.

● The print() function is a built-in function.

● 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='@')

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Conditional Statement
● Conditional Statement in Python perform different computations or actions depending on whether
a specific Boolean constraint evaluates to true or false.

● 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

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


if Statement
● if statement is the most simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not
● .Syntax:
if condition:
statement1
Statement2

● Example:
i = 10
if (i > 15):
print (“I am inside If Block")
print ("I am Not in if Block")

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


if-else Statement
● We can use the else statement with if statement to execute a block of code when the condition is
false.

● .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")

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Nested-if Statement
● A nested if is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement.

● .Syntax:
if (condition): # Executes this block if condition is true

else: # Executes this block if condition is false

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


if-elif-else ladder Statement
● The if-elif-else statement is used to conditionally execute a statement or a block of statements.
Conditions can be true or false, execute one thing when the condition is true, something else
when the condition is false.

● .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")

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Loop and Control statements
● Looping statements are executed sequentially. The first statement in a function is executed first,
followed by the second, and so on.

● Loops
While loop and For loop

● Loop control statements


○ continue
○ pass
○ break

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


while loop
● In python, while loop is used to execute a block of statements repeatedly until a given a condition
is satisfied

● Example
count = 0
while (count < 3):
count = count + 1
print("Hello Teachers")

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


for loop
● For loops are used for sequential traversal.

● Example
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


loop statements
● Break statement in Python is used to bring the control out of the loop when some external
condition is triggered.

● 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.

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Function in Python
● In Python, a function is a group of related statements that performs a specific task.

● 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))

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


OOP in Python
● Classes and objects are the two main aspects of object-oriented programming .
● Class :It is a design or blueprint to create an object.
● Object :In the real world, everything around us is an object. Each object has some properties and
functions.
○ FUNCTIONS are something which the object can do.
○ PROPERTIES are the qualities/characteristics of the object.

● 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.

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


Conclusion

© 2020 WhiteHat Education Technology Private Limited. All rights reserved


© 2020 WhiteHat Education Technology Private Limited. All rights reserved

You might also like