SlideShare a Scribd company logo
INTRODUCTION
TO
PYTHON
PROGRAMMING
-By
Ziyauddin Shaik
Btech (CSE)
(PART - 1)
Contents
โ€ข What is Python?
โ€ข Uses, features & flavours of Python
โ€ข Running Python
โ€ข Identifiers
โ€ข key words
โ€ข values & types
โ€ข Type casting
โ€ข Operators
โ€ข Functions
โ€ข Types of arguments
What is Python?
๏ƒ˜Python is a simple, general purpose, high level, Dynamic, Interpreted and object-
oriented programming language.
๏ƒ˜Python was developed by Guido Van Rossam in 1989 while working at National
Research Institute at Netherlands.
๏ƒ˜But officially python was made available to public in 1991. The official Date of
Birth for python is: Feb 20th 1991.
๏ƒ˜Guido developed python program language by taking almost all programming
features from different languages:
1. Functional Programming Features from C.
2. Object Oriented Programming Features from C++.
Uses of Python:
๏ƒ˜We can use everywhere .The most common important Application
areas are:
1. For developing Desktop Applications.
2. For developing Web Applications.
3. For developing Database Applications.
4. For developing Network programming Applications.
5. For developing Games.
6. For Data Analysis Applications.
7. For Machine Learning.
8. For developing Artificial Intelligence Applications.
9. For IoT etc.
Features of Python:
1. Simple and easy to learn
2. Freeware and Open Source
3. High Level Programming Language
4. Platform Independent
5. Portability
6. Dynamically Typed
7. Both procedure Oriented and Object Oriented
8. Interpreted
9. Extensible
10. Embedded
11. Extensive Library
Versions of Python :
๏ƒ˜ Python 1.0 in Jan 1994.
๏ƒ˜Python 2.0 in Oct 2000.
๏ƒ˜Python 3.0 in Dec 2008.
Flavors of Python:
NO Flavor description
1 Cpython It is the standard flavor of Python. It can be used to work with C language
Applications.
2 Jython (or) Jpython It is for Java Applications. It can run on JVM.
3 IronPython It is for C#.Net Platform.
4 PyPy The main advantage of PyPy is performance will be improved because JIT
compiler is Available inside PVM.
5 RubyPython For Ruby Platforms
6 AnacondaPython It is specially designed for handling large volume of data processing.
We Run Python Script in the following
ways:
1.Interactive Mode
2.Command Line
3.IDE Mode
1. Interactive Mode:
To enter in an interactive mode,
you will have to open Command
Prompt on your windows machine and
type โ€˜pythonโ€™ and press Enter.
Running Python :
2. Script Mode:
Using Script Mode, we can write our Python code in a separate file of any
editor in our Operating System.
๏ƒ˜Save it by .py extension.
๏ƒ˜Now open Command prompt and execute it by : python filename.py
Ex: python hello.py
You can write your own file name in place of โ€˜hello.pyโ€™.
3.Using IDE (Integrated Development Environment):
๏ƒ˜We can execute our Python code using a Graphical User Interface (GUI).
๏ƒ˜All you need to do is:
๏ƒ˜Click on Start button -> All Programs -> Python -> IDLE(Python GUI)
๏ƒ˜ Python Shell will be opened. Now click on File -> New Window.
๏ƒ˜A new Editor will be opened. Write our Python code here.
Identifiers :
๏ƒ˜A Name in python program is called Identifier
๏ƒ˜It can be variable Name (OR) Class Name (OR) Function Name
EX: a=10
Rules to Define Identifier in Python:
1. The only allowed characters in python are:
๏ƒ˜Alphabet symbols(Either lower or Upper case)
๏ƒ˜Digits(0 to 9)
๏ƒ˜Underscore Symbol(_)
2. Identifier should not start with Digit.
3.Identifers are Case Sensitive.
Reserved words ( or ) Key words :
๏ƒ˜Python Keywords are special reserved words that convey a special meaning
to the compiler/interpreter.
๏ƒ˜ Each keyword has a special meaning and a specific operation.
๏ƒ˜ These keywords can't be used as a variable.
๏ƒ˜ Following is the List of Python Keywords.
True False None and as
asset def class continue break
else finally elif del except
global for if from import
raise try or return pass
nonlocal in not is lambda
Value and Types:
Value:
๏ƒ˜A value is one of the basic thing in a
program ,like a letter or a number.
๏ƒ˜Some values we have seen so far are
2, 42.0, and 'Hello, World!โ€™.
๏ƒ˜These values belong to different
types:
Ex: 2 is an integer, 42.0 is a floating-
point number, and 'Hello, World!' is a
string
Types:
๏ƒ˜Data Type Represents the type of value
present inside a Variable.
๏ƒ˜In python we are not required to
specify the type explicitly. Based on Value
assigned the type will be assigned
automatically. Hence python is dynamic
typed language.
Python Contains the following inbuilt
Data Types:
1.Int
2.Float
3.Complex
4.Bool
5.Str
1.int:
int data type is used to represent whole numbers(integral values)
Ex: a=10
type(a) #int
We can represent int values in the following ways:
1.Decimal form
2.Binary form
3.Octal form
4.Hexa Decimal form
1.Decimal Form (Base-10):
๏ƒ˜It is the default number system.
๏ƒ˜The allowed digits are: 0 to 9
Ex: a=10
2. Binary Form (Base-2):
๏ƒ˜The allowed digits are: 0 & 1
๏ƒ˜Value should be prefixed with 0b or
0B.
Ex: a=0B1111
3.Octal Form (Base-8):
๏ƒ˜The allowed digits are: 0 to 7
๏ƒ˜Value should be prefixed with 0o or
0O
Ex: 0o123
4. Hexa Decimal Form ( Base -16):
๏ƒ˜The allowed digits are:0 to 9, a-f
(both lower and upper cases).
๏ƒ˜Values should be prefixed with 0x or 0X.
EX: a=0x10
2.float :
float data type is used to represent floating point values(decimal values)
Ex: f=1.234
type(f) #float
3. complex :
๏ƒ˜A complex number is of the form a+bj
Complex numbers are specified as <real part>+<imaginary part>j.
Note: โ€˜aโ€™ and โ€˜bโ€™ contain integers (or) floating point values.
EX: 3+5j
10+5.5j
0.5 + 0.1j
4.bool :
๏ƒ˜We can use this data type to represent Boolean values.
๏ƒ˜The only allowed values for this data type are: True and False
๏ƒ˜Internally python represents True as 1 and False as 0.
EX: b=True
type(b) -> bool
5.str :
๏ƒ˜A String is a sequence of characters enclosed within single /double quotes.
EX: s1=โ€˜Helloโ€™
s2=โ€œWelcomeโ€
๏ƒ˜By using single or double quotes we canโ€™t represent multi line string.
Type Casting :
๏ƒ˜Conversion of one type value to another is called typecasting.
๏ƒ˜The following are various inbuilt functions for type casting:
function input output
1.int() int(123.987) 123
2.float() float(10) 10.0
3.complex() complex(10) 10+0j
4.bool() bool(1) True
5.str() str(10) โ€˜10โ€™
Assignment Statements:
๏ƒ˜An assignment statement creates a new variable and gives it a value
Ex: message = 'And now for something completely differentโ€™
n = 17
pi = 3.1415926535897932
๏ƒ˜This example makes three assignments. The first assigns a string to a new
variable named message.
๏ƒ˜ the second gives the integer 17 to n.
๏ƒ˜ the third assigns the (approximate) value of ฯ€ to pi.
Operators:
๏ƒ˜Operator is a Symbol that perform certain operations.
๏ƒ˜Python provide the following Operators:
1.Arithmatic Operators
2.Relational Operators (or) Comparison Operators.
3.Logical Operators.
4.Bitwise Operators.
5. Assignment Operators.
6.Special Operators.
1. Arithmetic operators:
๏ƒ˜Python provides operators, which are special symbols that represent certain
operations like addition and multiplication etc.
๏ƒ˜One of the operator is Arithmetic operators.
Arithmetic Operators are:
1) + -> Addition
2) - -> Subtraction
3) * -> Multiplication
4) / -> Division
5) % -> Modulation
6) // -> Floor Division
7) ** -> Exponent (OR) Power Operator
Example :
a=10
b=2
print(โ€œa+b=โ€œ , a+b)
print(โ€œa-b=โ€œ , a-b)
print(โ€œa*b=โ€œ , a*b)
print(โ€œa/b=โ€œ , a/b)
print(โ€œa//b=โ€œ, a//b)
print(โ€œa%b=โ€œ , a%b)
print(โ€œa**b=โ€œ , a**b)
Output :
a+b=12
a-b=8
a*b=20
a/b=5.0
a//b=5
a%b=0
a**b=100
2.Relatonal (OR) Comparison Operators:
๏ƒ˜Comparison operators are used to comparing the value of the two operands and
returns Boolean true or false.
๏ƒ˜They return true if the condition is satisfied otherwise the return false.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the
condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
<> If the value of two operands is not equal, then the condition becomes true.
> If the first operand is greater than the second operand, then the condition
becomes true.
< If the first operand is less than the second operand, then the condition
becomes true.
Example :
a = 13
b = 33
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
print(a<>b)
Output:
False
True
False
True
False
True
True
๏ƒ˜We can also apply relational
operators for str types also.
Example :
a=โ€œhelloโ€
b=โ€œhelloโ€
print(โ€œa>b isโ€, a>b)
print(โ€œa>b isโ€, a>=b)
print(โ€œa>b isโ€, a<b)
print(โ€œa>b isโ€, a<=b)
Output :
a>b is False
a>=b is True
a<b is False
a<=b True
3.Logical Operators:(and,or,not)
๏ƒ˜The logical operators are used primarily in the expression evaluation to make
a decision.
1.For Boolean Types:
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the two
expressions, a โ†’ true, b โ†’ true => a and b โ†’ true.
Or If one of the expressions is true, then the condition will be true. If a and b are the two
expressions, a โ†’ true, b โ†’ false => a or b โ†’ true.
Not If an expression a is true then not (a) will be false and vice versa.
Example :
a=10
b=20
c=5
if a>b and a>c:
print(โ€œ a is biggestโ€)
elif b>c:
print(โ€œb Is biggestโ€)
else:
print(โ€œc is biggestโ€)
Output :
b is biggest
2.For non Boolean types:
X and Y:
If x is evaluates to false return X otherwise return Y.
Ex: 10 and 20 -> 20
0 and 20 -> 0
X or Y:
If x is evaluates to True return X otherwise return Y.
Ex: 10 and 20 ->10
0 or 20 ->20
not X :
0 means ->False
Non-zero means -> True
Empty string means -> False
If x is evaluates to False Then result is True otherwise False.
Ex : not 10 ->False
not 0 ->True
4.Bitwse Operators:
๏ƒ˜The bitwise operators perform bit by bit operation on the values of the two
operands.
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied
to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero otherwise the resulting
bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different otherwise the
resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the
right operand.
>> (right shift) The left operand is moved right by the number of bits present in the right
operand.
Program:
a = 10
b = 4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
Output:
0
14
-11
14
2
40
5.Assignment Operators:
๏ƒ˜The assignment operators are used to assign the value of the right expression
to the left operand.
perator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of
a + b into c
+= Add AND It adds right operand to the left operand and assign the result to left
operand
c += a is equivalent to c =
c + a
-= Subtract AND It subtracts right operand from the left operand and assign the result to left
operand
c -= a is equivalent to c =
c - a
*= Multiply AND It multiplies right operand with the left operand and assign the result to left
operand
c *= a is equivalent to c =
c * a
/= Divide AND It divides left operand with the right operand and assign the result to left
operand
c /= a is equivalent to c =
c / ac /= a is equivalent
to c = c / a
%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c =
c % a
**= Exponent AND Performs exponential (power) calculation on operators and assign value to
the left operand
c **= a is equivalent to c
= c ** a
//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c
= c // a
Program :
a = 21
b = 10
c = 0
c = a + b
print "Line 1 - Value of c is ", c
c += a
print "Line 2 - Value of c is ", c
c *= a
print "Line 3 - Value of c is ", c
c /= a
print "Line 4 - Value of c is ", c
c = 2
c %= a
print "Line 5 - Value of c is ", c
c **= a
print "Line 6 - Value of c is ", c
c //= a
print "Line 7 - Value of c is ", c
6.Special Operators:
1.Identity Operators:
๏ƒ˜We can use identity operators for
address comparison.
๏ƒ˜ the identity operators both are used
to check if two values are refer the
same part of the memory. It return
True (or) False
2.Membership Operators:
We can use Membership operators to
check whether the given object
present in the given collection.(It may
be string ,list,Tuple or Dict)
Operator Description
is It is evaluated to be true if the
reference present at both sides
point to the same object.
is not It is evaluated to be true if the
reference present at both sides do
not point to the same object.
Operator Description
in It is evaluated to be True if value is
found in the sequence (list, tuple,
or dictionary).
not in It is evaluated to be True if value is
not found in the sequence (list,
tuple, or dictionary).
Ex:
X=โ€œhello learning python is very easyโ€
print(โ€˜hโ€™ in X) ->True
print(โ€˜dโ€™ in X) -> False
print(โ€˜dโ€™ not in X) -> True
Print(โ€˜pythonโ€™ in X) ->True
Ex:
list1=[โ€œsunnyโ€,โ€bunnyโ€,โ€chinnyโ€,โ€pinnyโ€]
print(โ€œsunnyโ€ in list1) -> True
print(โ€œtunnyโ€ in list1) -> False
print(โ€œtunnyโ€ not in list1) -> True
String Operations: ( + , * )
๏ƒ˜The + operator performs string
concatenation, which means it joins
the strings by linking them end-to-
end.
Example:
first = 'throat'
second = 'warblerโ€™
Print( first + second)
Output :
throatwarbler
๏ƒ˜The * operator also works on
strings; it performs repetition.
Example:
Print( 'Spam'*3)
Output :
'SpamSpamSpam'
Functions :
What is Function: Function is a Block of code to perform specific Operation, it
only runs when it is called.
๏ƒ˜we can pass data to functions Known as parameters.
๏ƒ˜A Functions can return data as result.
Why Functions:
๏ƒ˜ Easy to Understand
๏ƒ˜Easy to Read
๏ƒ˜Easy to Debug
๏ƒ˜Code Reusability
๏ƒ˜Functions can make a program smaller by eliminating repetitive code. Later, if
you make a change, you only have to make it in one place.
Types Of Functions:
๏ƒ˜Python supports 2 types of functions:
1.Built in Functions
2.User Defined Functions
1.Built in Functions:
๏ƒ˜The functions which are coming along with python software automatically, are
called built in functions (or) predefined functions.
Ex :
id()
type()
input()
print()
eval()
2.User Defined Functions:
๏ƒ˜The functions which are developed by programmer explicitly according to
business requirements are called user defined functions.
Syntax to create user defined functions:
def function_name(parameters):
function Block
return value
๏ƒ˜In python a functions is defined using โ€œdefโ€ Keyword
๏ƒ˜A function accepts the parameter (argument), and they can be optional.
๏ƒ˜The function block is started with the colon (:), and block statements must
be at the same indentation.
๏ƒ˜The return statement is used to return the value. A function can have only
one return
Program :
def hello_world():
print(โ€œhello worldโ€)
hello_world()
function call
Math Functions:
๏ƒ˜Python has a math module that provides most of the familiar mathematical
functions.
๏ƒ˜ A module is a file that contains a collection of related functions.
๏ƒ˜Before we can use the functions in a module, we have to import it with an
import statement:
import modulename
EX: import math
๏ƒ˜This statement creates a module object named math.
๏ƒ˜we can access members by using module name.
modulename.variable
modulename.function()
Ex: import math
s=math.sqrt(4)
print(s)
Output : 2
Module Aliasing:
Syntax: import modulename as aliasname
Ex: import math as m
original module name alias name.
Ex: import math as m
s=m.sqrt(4)
print(s)
Output : 2
Member Aliasing:
Syntax:
from modulename import module1 as
aliasname,moule2 as aliasname,โ€ฆ.
Ex:
from math import sqrt as st, pow as pw
Ex :
s=st(4)
p=pw(2,2)
print(s)
print(p)
Coposition :
๏ƒ˜So far, we have looked at the elements of a programโ€”variables, expressions, and
statements.
๏ƒ˜One of the most useful features of programming languages is their ability to take
small building blocks and compose them.
๏ƒ˜ For example, the argument of a function can be any kind of expression, including
arithmetic operators:
๏ƒ˜ x = math.sin(degrees / 360.0 * 2 * math.pi)
๏ƒ˜And even function calls:
๏ƒ˜ x = math.exp(math.log(x+1))
๏ƒ˜ Almost anywhere you can put a value, you can put an arbitrary expression, with
one exception: the left side of an assignment statement has to be a variable
name.
๏ƒ˜ Any other expression on the left side is a syntax error (we will see exceptions to
this rule later).
๏ƒ˜ minutes = hours * 60 # right
FLOW OF EXECUTION:
๏ƒ˜To ensure that a function is defined before its first use, you have to know the
order statements run in, which is called the flow of execution.
๏ƒ˜ Execution always begins at the first statement of the program. Statements are
run one at a time, in order from top to bottom.
๏ƒ˜ Function definitions do not alter the flow of execution of the program, but
remember that statements inside the function donโ€™t run until the function is
called.
Parameters and Arguments:
๏ƒ˜Parameters are inputs to the function. if a function contains
parameters, then at the time of calling, compulsory we should provide
values otherwise, otherwise we will get error.
Ex: Write a function to take name of the student as input and print
message by name.
def wish(name):
print(โ€œ Hiโ€,name,โ€you look Gorgeous !โ€)
wish(โ€œ Selenaโ€)
wish(โ€œ Lisaโ€)
Output :
Hi Selena you look Gorgeous !
Hi Lisa you look Gorgeous !
Types of arguments:
def f1(a,b):
satement1
statement2
:
:
statement
f1(10,20)
There are 4 types of actual arguments in python:
1.positional Arguments
2.Keyword Arguments
3.Default Arguments
4.Variable Length Arguments
Formal arguments
Actual arguments
1.Positional Arguments:
๏ƒ˜These are the arguments passed to function in correct positional order.
EX: def add(a,b):
print(a+b)
add(10,20)
add(100,200)
OUTPUT: 30
300
๏ƒ˜The number of arguments and position of arguments must be matched. if we
change the order then result may be changed.
๏ƒ˜If we change the number of arguments then we will get error.
2.Keyword Arguments:
๏ƒ˜We can pass argument values by keyword i.e by parameter name.
EX: def wish(name,msg):
print(โ€œHelloโ€name,msg)
wish(name=โ€œSelena โ€,msg=โ€œGood Morningโ€)
wish(msg=โ€œGood Morningโ€,name=โ€œ Selena โ€)
OUTPUT:
Hello Selena Good Morning
Hello Selena Good Morning
๏ƒ˜Here the order of arguments is not important but the number of arguments
must be matched.
3.Default Arguments:
๏ƒ˜Some times we can provide default values for our positional
arguments.
EX:
def wish(name=โ€œGuest โ€):
print(โ€œhelloโ€,name,โ€good morningโ€)
wish(โ€œSelenaโ€)
wish()
OUTPUT:
Hello Selena good morning
Hello Guest good morning
๏ƒ˜If we are not passing any name then only default value will be
considered.
4.Vaiable Length Arguments:
๏ƒ˜Some times we can pass variable number of arguments to our function, such
type of arguments are called variable length arguments.
๏ƒ˜We can declare a variable length arguments with * symbol as follows.
EX: def f1(*N):
EX: def sum(*n):
total=0
for i in n:
total=total+n
print(โ€œthe sum =โ€,total)
sum()
sum(10)
sum(10,20)
sum(10,20,30,40)
OUTPUT:
the sum=0
the sum=10
the sum=30
the sum=100
Variables and Parameters are local:
1.Local Variables:
๏ƒ˜The variables which are declared inside a
function are called local variables.
๏ƒ˜Local variables are available only for the
function in which we declared it. i.e from out
side of function we cannot access.
EX:
def f1():
a=10 ->Local variable
print(a) -> valid
def f2():
print(a) ->Invalid
f1()
f2()
NameError: name โ€˜aโ€™ is not defined
2. Global Variables :
๏ƒ˜The variables which are declared
out side of function are called global
variables.
๏ƒ˜These variables can be accessed in
all functions of that module.
EX:
a=10 -> global variable
def f1():
print(a) -> valid
def f2():
print(a) ->valid
f1()
f2()
Fruitful Functions : Void Functions:
๏ƒ˜Which are the functions return
values are called fruitful functions.
๏ƒ˜The function return values by
executing โ€œreturnโ€ statement.
EX: def f1(a,b):
c=a+b
return c
sum=f1(10,20)
print(sum)
OUTPUT:
30
๏ƒ˜Which are the functions are not
return values are called void functions
.
EX:
def f1(a,b):
c=a+b
print( c )
f1(10,20)
OUTPUT:
30
# THANK YOU

More Related Content

Similar to Introduction to python programming ( part-1) (20)

PPTX
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
ย 
PPTX
Introduction to learn and Python Interpreter
Alamelu
ย 
PPTX
chapter_5_ppt_em_220247.pptx
RAJAMURUGANAMECAPCSE
ย 
PDF
learn basic to advance C Programming Notes
bhagadeakshay97
ย 
PPTX
Programming presentation
Fiaz Khokhar
ย 
PDF
Python-01| Fundamentals
Mohd Sajjad
ย 
PPTX
Python Introduction
vikram mahendra
ย 
PPTX
C++ lecture 01
HNDE Labuduwa Galle
ย 
PPTX
Introduction To Python.pptx
Anum Zehra
ย 
PPTX
Python introduction towards data science
deepak teja
ย 
PPT
Python Programming Introduction demo.ppt
JohariNawab
ย 
PDF
An overview on python commands for solving the problems
Ravikiran708913
ย 
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
ย 
PPS
C programming session 02
Dushmanta Nath
ย 
PPTX
Python basics
Manisha Gholve
ย 
PPTX
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
ย 
PPTX
PPt Revision of the basics of python1.pptx
tcsonline1222
ย 
PDF
Oop with c++ notes unit 01 introduction
Ananda Kumar HN
ย 
PPTX
C++ programming language basic to advance level
sajjad ali khan
ย 
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
yuvarajkumar334
ย 
Introduction to learn and Python Interpreter
Alamelu
ย 
chapter_5_ppt_em_220247.pptx
RAJAMURUGANAMECAPCSE
ย 
learn basic to advance C Programming Notes
bhagadeakshay97
ย 
Programming presentation
Fiaz Khokhar
ย 
Python-01| Fundamentals
Mohd Sajjad
ย 
Python Introduction
vikram mahendra
ย 
C++ lecture 01
HNDE Labuduwa Galle
ย 
Introduction To Python.pptx
Anum Zehra
ย 
Python introduction towards data science
deepak teja
ย 
Python Programming Introduction demo.ppt
JohariNawab
ย 
An overview on python commands for solving the problems
Ravikiran708913
ย 
Introduction to C Language - Version 1.0 by Mark John Lado
Mark John Lado, MIT
ย 
C programming session 02
Dushmanta Nath
ย 
Python basics
Manisha Gholve
ย 
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
ย 
PPt Revision of the basics of python1.pptx
tcsonline1222
ย 
Oop with c++ notes unit 01 introduction
Ananda Kumar HN
ย 
C++ programming language basic to advance level
sajjad ali khan
ย 

More from Ziyauddin Shaik (7)

PPTX
Operating Systems
Ziyauddin Shaik
ย 
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
ย 
PPTX
Introduction to python programming ( part-3 )
Ziyauddin Shaik
ย 
PPTX
Introduction to python programming ( part-2 )
Ziyauddin Shaik
ย 
PPTX
Product Design
Ziyauddin Shaik
ย 
PPTX
Capsule Endoscopy
Ziyauddin Shaik
ย 
PPTX
Amazon Go : The future of shopping
Ziyauddin Shaik
ย 
Operating Systems
Ziyauddin Shaik
ย 
Webinar : P, NP, NP-Hard , NP - Complete problems
Ziyauddin Shaik
ย 
Introduction to python programming ( part-3 )
Ziyauddin Shaik
ย 
Introduction to python programming ( part-2 )
Ziyauddin Shaik
ย 
Product Design
Ziyauddin Shaik
ย 
Capsule Endoscopy
Ziyauddin Shaik
ย 
Amazon Go : The future of shopping
Ziyauddin Shaik
ย 
Ad

Recently uploaded (20)

PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
PDF
Rewards and Recognition (2).pdf
ethan Talor
ย 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
ย 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
ย 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
Rewards and Recognition (2).pdf
ethan Talor
ย 
Introduction to web development | MERN Stack
JosephLiyon
ย 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
ย 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
ย 
Ad

Introduction to python programming ( part-1)

  • 2. Contents โ€ข What is Python? โ€ข Uses, features & flavours of Python โ€ข Running Python โ€ข Identifiers โ€ข key words โ€ข values & types โ€ข Type casting โ€ข Operators โ€ข Functions โ€ข Types of arguments
  • 3. What is Python? ๏ƒ˜Python is a simple, general purpose, high level, Dynamic, Interpreted and object- oriented programming language. ๏ƒ˜Python was developed by Guido Van Rossam in 1989 while working at National Research Institute at Netherlands. ๏ƒ˜But officially python was made available to public in 1991. The official Date of Birth for python is: Feb 20th 1991. ๏ƒ˜Guido developed python program language by taking almost all programming features from different languages: 1. Functional Programming Features from C. 2. Object Oriented Programming Features from C++.
  • 4. Uses of Python: ๏ƒ˜We can use everywhere .The most common important Application areas are: 1. For developing Desktop Applications. 2. For developing Web Applications. 3. For developing Database Applications. 4. For developing Network programming Applications. 5. For developing Games. 6. For Data Analysis Applications. 7. For Machine Learning. 8. For developing Artificial Intelligence Applications. 9. For IoT etc.
  • 5. Features of Python: 1. Simple and easy to learn 2. Freeware and Open Source 3. High Level Programming Language 4. Platform Independent 5. Portability 6. Dynamically Typed 7. Both procedure Oriented and Object Oriented 8. Interpreted 9. Extensible 10. Embedded 11. Extensive Library Versions of Python : ๏ƒ˜ Python 1.0 in Jan 1994. ๏ƒ˜Python 2.0 in Oct 2000. ๏ƒ˜Python 3.0 in Dec 2008.
  • 6. Flavors of Python: NO Flavor description 1 Cpython It is the standard flavor of Python. It can be used to work with C language Applications. 2 Jython (or) Jpython It is for Java Applications. It can run on JVM. 3 IronPython It is for C#.Net Platform. 4 PyPy The main advantage of PyPy is performance will be improved because JIT compiler is Available inside PVM. 5 RubyPython For Ruby Platforms 6 AnacondaPython It is specially designed for handling large volume of data processing.
  • 7. We Run Python Script in the following ways: 1.Interactive Mode 2.Command Line 3.IDE Mode 1. Interactive Mode: To enter in an interactive mode, you will have to open Command Prompt on your windows machine and type โ€˜pythonโ€™ and press Enter. Running Python :
  • 8. 2. Script Mode: Using Script Mode, we can write our Python code in a separate file of any editor in our Operating System. ๏ƒ˜Save it by .py extension. ๏ƒ˜Now open Command prompt and execute it by : python filename.py Ex: python hello.py You can write your own file name in place of โ€˜hello.pyโ€™.
  • 9. 3.Using IDE (Integrated Development Environment): ๏ƒ˜We can execute our Python code using a Graphical User Interface (GUI). ๏ƒ˜All you need to do is: ๏ƒ˜Click on Start button -> All Programs -> Python -> IDLE(Python GUI) ๏ƒ˜ Python Shell will be opened. Now click on File -> New Window. ๏ƒ˜A new Editor will be opened. Write our Python code here.
  • 10. Identifiers : ๏ƒ˜A Name in python program is called Identifier ๏ƒ˜It can be variable Name (OR) Class Name (OR) Function Name EX: a=10 Rules to Define Identifier in Python: 1. The only allowed characters in python are: ๏ƒ˜Alphabet symbols(Either lower or Upper case) ๏ƒ˜Digits(0 to 9) ๏ƒ˜Underscore Symbol(_) 2. Identifier should not start with Digit. 3.Identifers are Case Sensitive.
  • 11. Reserved words ( or ) Key words : ๏ƒ˜Python Keywords are special reserved words that convey a special meaning to the compiler/interpreter. ๏ƒ˜ Each keyword has a special meaning and a specific operation. ๏ƒ˜ These keywords can't be used as a variable. ๏ƒ˜ Following is the List of Python Keywords. True False None and as asset def class continue break else finally elif del except global for if from import raise try or return pass nonlocal in not is lambda
  • 12. Value and Types: Value: ๏ƒ˜A value is one of the basic thing in a program ,like a letter or a number. ๏ƒ˜Some values we have seen so far are 2, 42.0, and 'Hello, World!โ€™. ๏ƒ˜These values belong to different types: Ex: 2 is an integer, 42.0 is a floating- point number, and 'Hello, World!' is a string Types: ๏ƒ˜Data Type Represents the type of value present inside a Variable. ๏ƒ˜In python we are not required to specify the type explicitly. Based on Value assigned the type will be assigned automatically. Hence python is dynamic typed language. Python Contains the following inbuilt Data Types: 1.Int 2.Float 3.Complex 4.Bool 5.Str
  • 13. 1.int: int data type is used to represent whole numbers(integral values) Ex: a=10 type(a) #int We can represent int values in the following ways: 1.Decimal form 2.Binary form 3.Octal form 4.Hexa Decimal form
  • 14. 1.Decimal Form (Base-10): ๏ƒ˜It is the default number system. ๏ƒ˜The allowed digits are: 0 to 9 Ex: a=10 2. Binary Form (Base-2): ๏ƒ˜The allowed digits are: 0 & 1 ๏ƒ˜Value should be prefixed with 0b or 0B. Ex: a=0B1111 3.Octal Form (Base-8): ๏ƒ˜The allowed digits are: 0 to 7 ๏ƒ˜Value should be prefixed with 0o or 0O Ex: 0o123 4. Hexa Decimal Form ( Base -16): ๏ƒ˜The allowed digits are:0 to 9, a-f (both lower and upper cases). ๏ƒ˜Values should be prefixed with 0x or 0X. EX: a=0x10
  • 15. 2.float : float data type is used to represent floating point values(decimal values) Ex: f=1.234 type(f) #float 3. complex : ๏ƒ˜A complex number is of the form a+bj Complex numbers are specified as <real part>+<imaginary part>j. Note: โ€˜aโ€™ and โ€˜bโ€™ contain integers (or) floating point values. EX: 3+5j 10+5.5j 0.5 + 0.1j
  • 16. 4.bool : ๏ƒ˜We can use this data type to represent Boolean values. ๏ƒ˜The only allowed values for this data type are: True and False ๏ƒ˜Internally python represents True as 1 and False as 0. EX: b=True type(b) -> bool 5.str : ๏ƒ˜A String is a sequence of characters enclosed within single /double quotes. EX: s1=โ€˜Helloโ€™ s2=โ€œWelcomeโ€ ๏ƒ˜By using single or double quotes we canโ€™t represent multi line string.
  • 17. Type Casting : ๏ƒ˜Conversion of one type value to another is called typecasting. ๏ƒ˜The following are various inbuilt functions for type casting: function input output 1.int() int(123.987) 123 2.float() float(10) 10.0 3.complex() complex(10) 10+0j 4.bool() bool(1) True 5.str() str(10) โ€˜10โ€™
  • 18. Assignment Statements: ๏ƒ˜An assignment statement creates a new variable and gives it a value Ex: message = 'And now for something completely differentโ€™ n = 17 pi = 3.1415926535897932 ๏ƒ˜This example makes three assignments. The first assigns a string to a new variable named message. ๏ƒ˜ the second gives the integer 17 to n. ๏ƒ˜ the third assigns the (approximate) value of ฯ€ to pi.
  • 19. Operators: ๏ƒ˜Operator is a Symbol that perform certain operations. ๏ƒ˜Python provide the following Operators: 1.Arithmatic Operators 2.Relational Operators (or) Comparison Operators. 3.Logical Operators. 4.Bitwise Operators. 5. Assignment Operators. 6.Special Operators.
  • 20. 1. Arithmetic operators: ๏ƒ˜Python provides operators, which are special symbols that represent certain operations like addition and multiplication etc. ๏ƒ˜One of the operator is Arithmetic operators. Arithmetic Operators are: 1) + -> Addition 2) - -> Subtraction 3) * -> Multiplication 4) / -> Division 5) % -> Modulation 6) // -> Floor Division 7) ** -> Exponent (OR) Power Operator
  • 21. Example : a=10 b=2 print(โ€œa+b=โ€œ , a+b) print(โ€œa-b=โ€œ , a-b) print(โ€œa*b=โ€œ , a*b) print(โ€œa/b=โ€œ , a/b) print(โ€œa//b=โ€œ, a//b) print(โ€œa%b=โ€œ , a%b) print(โ€œa**b=โ€œ , a**b) Output : a+b=12 a-b=8 a*b=20 a/b=5.0 a//b=5 a%b=0 a**b=100
  • 22. 2.Relatonal (OR) Comparison Operators: ๏ƒ˜Comparison operators are used to comparing the value of the two operands and returns Boolean true or false. ๏ƒ˜They return true if the condition is satisfied otherwise the return false. Operator Description == If the value of two operands is equal, then the condition becomes true. != If the value of two operands is not equal then the condition becomes true. <= If the first operand is less than or equal to the second operand, then the condition becomes true. >= If the first operand is greater than or equal to the second operand, then the condition becomes true. <> If the value of two operands is not equal, then the condition becomes true. > If the first operand is greater than the second operand, then the condition becomes true. < If the first operand is less than the second operand, then the condition becomes true.
  • 23. Example : a = 13 b = 33 print(a > b) print(a < b) print(a == b) print(a != b) print(a >= b) print(a <= b) print(a<>b) Output: False True False True False True True ๏ƒ˜We can also apply relational operators for str types also. Example : a=โ€œhelloโ€ b=โ€œhelloโ€ print(โ€œa>b isโ€, a>b) print(โ€œa>b isโ€, a>=b) print(โ€œa>b isโ€, a<b) print(โ€œa>b isโ€, a<=b) Output : a>b is False a>=b is True a<b is False a<=b True
  • 24. 3.Logical Operators:(and,or,not) ๏ƒ˜The logical operators are used primarily in the expression evaluation to make a decision. 1.For Boolean Types: Operator Description and If both the expression are true, then the condition will be true. If a and b are the two expressions, a โ†’ true, b โ†’ true => a and b โ†’ true. Or If one of the expressions is true, then the condition will be true. If a and b are the two expressions, a โ†’ true, b โ†’ false => a or b โ†’ true. Not If an expression a is true then not (a) will be false and vice versa. Example : a=10 b=20 c=5 if a>b and a>c: print(โ€œ a is biggestโ€) elif b>c: print(โ€œb Is biggestโ€) else: print(โ€œc is biggestโ€) Output : b is biggest
  • 25. 2.For non Boolean types: X and Y: If x is evaluates to false return X otherwise return Y. Ex: 10 and 20 -> 20 0 and 20 -> 0 X or Y: If x is evaluates to True return X otherwise return Y. Ex: 10 and 20 ->10 0 or 20 ->20 not X : 0 means ->False Non-zero means -> True Empty string means -> False If x is evaluates to False Then result is True otherwise False. Ex : not 10 ->False not 0 ->True
  • 26. 4.Bitwse Operators: ๏ƒ˜The bitwise operators perform bit by bit operation on the values of the two operands. Operator Description & (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied. | (binary or) The resulting bit will be 0 if both the bits are zero otherwise the resulting bit will be 1. ^ (binary xor) The resulting bit will be 1 if both the bits are different otherwise the resulting bit will be 0. ~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa. << (left shift) The left operand value is moved left by the number of bits present in the right operand. >> (right shift) The left operand is moved right by the number of bits present in the right operand. Program: a = 10 b = 4 print(a & b) print(a | b) print(~a) print(a ^ b) print(a >> 2) print(a << 2) Output: 0 14 -11 14 2 40
  • 27. 5.Assignment Operators: ๏ƒ˜The assignment operators are used to assign the value of the right expression to the left operand. perator Description Example = Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c += Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a *= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a %= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a Program : a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2 c %= a print "Line 5 - Value of c is ", c c **= a print "Line 6 - Value of c is ", c c //= a print "Line 7 - Value of c is ", c
  • 28. 6.Special Operators: 1.Identity Operators: ๏ƒ˜We can use identity operators for address comparison. ๏ƒ˜ the identity operators both are used to check if two values are refer the same part of the memory. It return True (or) False 2.Membership Operators: We can use Membership operators to check whether the given object present in the given collection.(It may be string ,list,Tuple or Dict) Operator Description is It is evaluated to be true if the reference present at both sides point to the same object. is not It is evaluated to be true if the reference present at both sides do not point to the same object. Operator Description in It is evaluated to be True if value is found in the sequence (list, tuple, or dictionary). not in It is evaluated to be True if value is not found in the sequence (list, tuple, or dictionary).
  • 29. Ex: X=โ€œhello learning python is very easyโ€ print(โ€˜hโ€™ in X) ->True print(โ€˜dโ€™ in X) -> False print(โ€˜dโ€™ not in X) -> True Print(โ€˜pythonโ€™ in X) ->True Ex: list1=[โ€œsunnyโ€,โ€bunnyโ€,โ€chinnyโ€,โ€pinnyโ€] print(โ€œsunnyโ€ in list1) -> True print(โ€œtunnyโ€ in list1) -> False print(โ€œtunnyโ€ not in list1) -> True
  • 30. String Operations: ( + , * ) ๏ƒ˜The + operator performs string concatenation, which means it joins the strings by linking them end-to- end. Example: first = 'throat' second = 'warblerโ€™ Print( first + second) Output : throatwarbler ๏ƒ˜The * operator also works on strings; it performs repetition. Example: Print( 'Spam'*3) Output : 'SpamSpamSpam'
  • 31. Functions : What is Function: Function is a Block of code to perform specific Operation, it only runs when it is called. ๏ƒ˜we can pass data to functions Known as parameters. ๏ƒ˜A Functions can return data as result. Why Functions: ๏ƒ˜ Easy to Understand ๏ƒ˜Easy to Read ๏ƒ˜Easy to Debug ๏ƒ˜Code Reusability ๏ƒ˜Functions can make a program smaller by eliminating repetitive code. Later, if you make a change, you only have to make it in one place.
  • 32. Types Of Functions: ๏ƒ˜Python supports 2 types of functions: 1.Built in Functions 2.User Defined Functions 1.Built in Functions: ๏ƒ˜The functions which are coming along with python software automatically, are called built in functions (or) predefined functions. Ex : id() type() input() print() eval()
  • 33. 2.User Defined Functions: ๏ƒ˜The functions which are developed by programmer explicitly according to business requirements are called user defined functions. Syntax to create user defined functions: def function_name(parameters): function Block return value ๏ƒ˜In python a functions is defined using โ€œdefโ€ Keyword ๏ƒ˜A function accepts the parameter (argument), and they can be optional. ๏ƒ˜The function block is started with the colon (:), and block statements must be at the same indentation. ๏ƒ˜The return statement is used to return the value. A function can have only one return Program : def hello_world(): print(โ€œhello worldโ€) hello_world() function call
  • 34. Math Functions: ๏ƒ˜Python has a math module that provides most of the familiar mathematical functions. ๏ƒ˜ A module is a file that contains a collection of related functions. ๏ƒ˜Before we can use the functions in a module, we have to import it with an import statement: import modulename EX: import math ๏ƒ˜This statement creates a module object named math. ๏ƒ˜we can access members by using module name. modulename.variable modulename.function() Ex: import math s=math.sqrt(4) print(s) Output : 2
  • 35. Module Aliasing: Syntax: import modulename as aliasname Ex: import math as m original module name alias name. Ex: import math as m s=m.sqrt(4) print(s) Output : 2 Member Aliasing: Syntax: from modulename import module1 as aliasname,moule2 as aliasname,โ€ฆ. Ex: from math import sqrt as st, pow as pw Ex : s=st(4) p=pw(2,2) print(s) print(p)
  • 36. Coposition : ๏ƒ˜So far, we have looked at the elements of a programโ€”variables, expressions, and statements. ๏ƒ˜One of the most useful features of programming languages is their ability to take small building blocks and compose them. ๏ƒ˜ For example, the argument of a function can be any kind of expression, including arithmetic operators: ๏ƒ˜ x = math.sin(degrees / 360.0 * 2 * math.pi) ๏ƒ˜And even function calls: ๏ƒ˜ x = math.exp(math.log(x+1)) ๏ƒ˜ Almost anywhere you can put a value, you can put an arbitrary expression, with one exception: the left side of an assignment statement has to be a variable name. ๏ƒ˜ Any other expression on the left side is a syntax error (we will see exceptions to this rule later). ๏ƒ˜ minutes = hours * 60 # right
  • 37. FLOW OF EXECUTION: ๏ƒ˜To ensure that a function is defined before its first use, you have to know the order statements run in, which is called the flow of execution. ๏ƒ˜ Execution always begins at the first statement of the program. Statements are run one at a time, in order from top to bottom. ๏ƒ˜ Function definitions do not alter the flow of execution of the program, but remember that statements inside the function donโ€™t run until the function is called.
  • 38. Parameters and Arguments: ๏ƒ˜Parameters are inputs to the function. if a function contains parameters, then at the time of calling, compulsory we should provide values otherwise, otherwise we will get error. Ex: Write a function to take name of the student as input and print message by name. def wish(name): print(โ€œ Hiโ€,name,โ€you look Gorgeous !โ€) wish(โ€œ Selenaโ€) wish(โ€œ Lisaโ€) Output : Hi Selena you look Gorgeous ! Hi Lisa you look Gorgeous !
  • 39. Types of arguments: def f1(a,b): satement1 statement2 : : statement f1(10,20) There are 4 types of actual arguments in python: 1.positional Arguments 2.Keyword Arguments 3.Default Arguments 4.Variable Length Arguments Formal arguments Actual arguments
  • 40. 1.Positional Arguments: ๏ƒ˜These are the arguments passed to function in correct positional order. EX: def add(a,b): print(a+b) add(10,20) add(100,200) OUTPUT: 30 300 ๏ƒ˜The number of arguments and position of arguments must be matched. if we change the order then result may be changed. ๏ƒ˜If we change the number of arguments then we will get error.
  • 41. 2.Keyword Arguments: ๏ƒ˜We can pass argument values by keyword i.e by parameter name. EX: def wish(name,msg): print(โ€œHelloโ€name,msg) wish(name=โ€œSelena โ€,msg=โ€œGood Morningโ€) wish(msg=โ€œGood Morningโ€,name=โ€œ Selena โ€) OUTPUT: Hello Selena Good Morning Hello Selena Good Morning ๏ƒ˜Here the order of arguments is not important but the number of arguments must be matched.
  • 42. 3.Default Arguments: ๏ƒ˜Some times we can provide default values for our positional arguments. EX: def wish(name=โ€œGuest โ€): print(โ€œhelloโ€,name,โ€good morningโ€) wish(โ€œSelenaโ€) wish() OUTPUT: Hello Selena good morning Hello Guest good morning ๏ƒ˜If we are not passing any name then only default value will be considered.
  • 43. 4.Vaiable Length Arguments: ๏ƒ˜Some times we can pass variable number of arguments to our function, such type of arguments are called variable length arguments. ๏ƒ˜We can declare a variable length arguments with * symbol as follows. EX: def f1(*N): EX: def sum(*n): total=0 for i in n: total=total+n print(โ€œthe sum =โ€,total) sum() sum(10) sum(10,20) sum(10,20,30,40) OUTPUT: the sum=0 the sum=10 the sum=30 the sum=100
  • 44. Variables and Parameters are local: 1.Local Variables: ๏ƒ˜The variables which are declared inside a function are called local variables. ๏ƒ˜Local variables are available only for the function in which we declared it. i.e from out side of function we cannot access. EX: def f1(): a=10 ->Local variable print(a) -> valid def f2(): print(a) ->Invalid f1() f2() NameError: name โ€˜aโ€™ is not defined 2. Global Variables : ๏ƒ˜The variables which are declared out side of function are called global variables. ๏ƒ˜These variables can be accessed in all functions of that module. EX: a=10 -> global variable def f1(): print(a) -> valid def f2(): print(a) ->valid f1() f2()
  • 45. Fruitful Functions : Void Functions: ๏ƒ˜Which are the functions return values are called fruitful functions. ๏ƒ˜The function return values by executing โ€œreturnโ€ statement. EX: def f1(a,b): c=a+b return c sum=f1(10,20) print(sum) OUTPUT: 30 ๏ƒ˜Which are the functions are not return values are called void functions . EX: def f1(a,b): c=a+b print( c ) f1(10,20) OUTPUT: 30