You are on page 1of 26

Python Basics & Advanced

1 Python Basics
o Python – Overview
2 Python Advanced
o Python - Classes/Objects
o Python - Environment Setup
Content
o Python - Basic Syntax o Python - Reg Expressions

o Python - Variable Types o Python - CGI Programming

o Python - Basic Operators o Python - Database Access

o Python - Decision Making o Python - Networking


o Python - Loops o Python - Sending Email
o Python - Numbers o Python - Multithreading
o Python - Strings o Python - XML Processing
o Python - Lists o Python - GUI Programming
o Python - Tuples
o Python - Dictionary
o Python - Date & Time
o Python - Functions
o Python - Modules
o Python - Files I/O
o Python - Exception
Python Basics & Advanced
Quick Recap

Introduction to Python & Its characteristics Python Variables

01 Covered the basic details of Python and why it is so


popular. 02 Rules for defining the Python Variables and how to assign
values to these Variables.

Python Datatype , Operators and Conditional


Loops and Function in Python
Statement

03 During this we gone through Numbers, String and Python


collections (List, Tuple, Set, Dictionary , Operator and
Conditional statement (if , elif, nested if)
04 Focused Area will be Python loop & Function
o Quick recap
o Decision making in Python
o Loops in Python

Day-4 o While Loop


o For Loop
Agenda o Loop control statement.
o Functions in Python.
Python Basics
Quiz

Quiz1. What will be the output of the following Python code?


l=list('HELLO')
'first={0[0]}, third={0[2]}'.format(l)

a) ‘first=H, third=L’
b) ‘first=0, third=2’
c) Error
d) ‘first=0, third=L’
Python Basics
Quiz

Quiz1. What will be the output of the following Python code?


l=list('HELLO')
'first={0[0]}, third={0[2]}'.format(l)

a) ‘first=H, third=L’
b) ‘first=0, third=2’
c) Error
d) ‘first=0, third=L’

The correct answer is a


Explanation: Explanation: In the code shown above, the value for first is substituted by l[0], that is H and the value for third is substituted by l[2], that
is L. Hence the output of the code shown above is: ‘first=H, third=L’. The list l= [‘H’, ‘E’, ‘L’, ‘L’, ‘O’].
Python Basics
Quiz

Quiz2. What data type is the object below ?


L = [1, 23, ‘hello’, 1]

o List

o Dictionary

o Tuple

o Array
Python Basics
Quiz

Quiz2. What data type is the object below ?


L = [1, 23, ‘hello’, 1]

o List

o Dictionary

o Tuple

o Array

The correct answer is A


Explanation: [ ] defines a list
Python Basics
Quiz

Quiz3. What is the output of the following program :


i=0
while i < 3:
print( i)
i += 1
else:
print(0)

a) 01230
b) 0120
c) 012
d) Error
Python Basics
Quiz

Quiz3. What is the output of the following program :


i=0
while i < 3:
print i
i += 1
else:
print 0

a) 01230
b) 0120
c) 012
d) Error

The correct answer is B


Explanation: The else part is executed when the condition in the while statement is false.
Python Basics
Quiz

Quiz4. What will be the output of the following Python statement?

>>>"a"+"bc"

a) a
b) bc
c) bca
d) abc
Python Basics
Quiz

Quiz4. What will be the output of the following Python statement?

>>>"a"+"bc"

a) a
b) bc
c) bca
d) abc

The correct answer is D


Explanation: + operator is concatenation operator.
Python Basics
Quiz

Quiz5. What will be the output for below given program?

var1 = 'Hello BigDataFactory!'


var2 = "BigDataFactoryForALL"

print("var1[0]: ", var1[0]) # statement 1


print("var2[1:5]: ", var2[1:5]) # statement 2
Python Basics
Python Loops

Python has two primitive loop commands:


• With the while loop we can execute a set of statements as long as a condition is true.
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary,
a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Example –While loop Example –For Loop


i=1 fruits=['apple', 'banana', 'cheery']
while i < 6: for x in fruits:
print(i) print(x)
i=i+1
Python Basics
Python Loops- while
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

Example with else statement


Example i=1
i=1 while i < 6:
print(i)
while i < 6:
i=i+1
print(i) else:
i=i+1 print("i is no longer less than 6")
Python Basics
Python Loops- for

Flow Diagram Examples

for letter in 'Python':


print('Current Letter :', letter)

fruits = ['banana', 'apple', 'mango’]


for fruit in fruits:
print('Current fruit :', fruit)
print("Good bye!")
Python Basics
Loop Control Statements-break

Sr.N Control Statement & Description


o.
Flow Diagram -Break Example- Output
1 break statement Terminates the loop
statement and transfers execution to the for letter in 'Python':
statement immediately following the loop. if letter == 'h':
break
print('Current Letter :', letter)

var = 10
while var > 0:
2 continue statement Causes the loop to print('Current variable value :', var)
skip the remainder of its body and var = var -1
immediately retest its condition prior to if var == 5:
reiterating. break
print("Good bye!")

3 pass statement The pass statement in


Python is used when a statement is
required syntactically but you do not want
any command or code to execute.
Python Basics
Loop Control statement- Continue
It returns the control to the beginning of the while loop.. The continue statement rejects all the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Output
Example
for letter in 'Python':
if letter == 'h':
continue
print('Current Letter :', letter)

var = 10
while var > 0:
var = var -1
if var == 5:
continue
print('Current variable value :', var)
print("Good bye!")
Python Basics
Loop Control Statement- Pass

It is used when a statement is required syntactically but you do not want any command or code to execute.

Example Output
for letter in 'Python':
if letter == 'h':
pass
print('This is pass block’)
print('Current Letter :', letter)

print "Good bye!"


Python Basics
Range Function ()

This function yields a sequence of numbers. When called with one argument, say n, it creates a sequence of numbers
from 0 to n-1.

print(list(range(10)))

print(list(range(2,7)))

print(list(range(2,12,2)))

print(list(range(12,2,-2)))
Python Basics
Function

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity
for your application and a high degree of code reusing.

Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are
called user-defined functions.

Syntax for defining a Calling a function:- Arguments in funcation


function:- def my_function(fname):
def functionname( parameters ): def my_funtion(): print(fname + " Machine Learning")
print("Hello from a function")
"function_docstring" my_function("Emil")
my_funtion()
function_suite my_function("Tobias")
return [expression] my_function("Linus")
Python Basics
Function

Default Parameter Value Returning a Value


def my_function(country="Norway"):
print("I am from " + country) def my_function(x):
my_function("Sweden") return 5*x
my_function("India") print(my_function(3))
my_function() print(my_function(4))
my_function("Brazil") print(my_function(5))

The Pass Statement:-


Example-pass
function definitions cannot be empty, but if you for some
reason have a function definition with no content, put in
def myfunction():
the pass statement to avoid getting an error.
pass
Python Basics
Anonymous Functions

These functions are called anonymous because they are not declared in the standard manner by using
the def keyword. You can use the lambda keyword to create small anonymous functions.

Syntax Output
lambda [arg1 [,arg2,.....argn]]:expression

Example
# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2;

# Now you can call sum as a function


print("Value of total : ", sum( 10, 20 ))
print("Value of total : ", sum( 20, 20 ))
Python Basics
Local Variable Vs Global Variable

All variables in a program may not be accessible at all locations in that program. This depends on where you have
declared a variable.
• Global variables
• Local variables
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
Example:
total = 0; # This is global variable. Output
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print("Inside the function local total : ", total)
return total;

# Now you can call sum function


sum( 10, 20 );
print("Outside the function global total : ", total)
Python Basics
Numpy

A library for Python, NumPy lets you work with huge, multidimensional matrices and arrays. Along with that, it
provides a mechanism of high-level functions to perform mathematical operations on these structures

Feature

o Multidimensional arrays.
o Functions and operators for these arrays.
o ndarray- n-dimensional arrays.
o Fourier transforms and shapes manipulation.
o Linear algebra and random number generation.
Thank you

You might also like