Bscit Python 3 Org
Bscit Python 3 Org
Functions
Python has many components like,
Functions
Python allows large program to be divided into basic
blocks called functions.
Classes
Classes can comprise many objects, functions, codes
etc.
Modules
A files that contains definition of functions and classes
Packages
Many modules can be combined in packages
A function is a block of code which only runs when
it is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Creating a Function
In Python a function is defined using the def
keyword:
Example
def my_function():
print("Hello from a function")
Python supports the use of three types of
functions:
Python Built-in functions
Already created, predefined function
User defined function
A function created by users as per the requirements
Anonymous function
A function having no name
Function has three parts:
Name
Every function has a name that identifies the code to
be executed.
Function rules follow the same rules for variable
names.
Parameters
A function must be called with a certain number of
parameters, and each parameters must be the correct
type.
Some functions do not accept any parametes.
Result Type
A function returns a value to its caller.
name=“Heera”
def display():
print(“My name is“, name)
display()
Calling a Function
To call a function, use
the function name
followed by parenthesis:
Example
def my_function():
print("Hello from a function")
my_function()
Arguments
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
Parameters or Arguments?
The terms parameter and argument can
be used for the same thing: information that
are passed into a function.
A parameter is the variable listed inside
the parentheses in the function definition.
An argument is the value that is sent to
the function when it is called.
Keyword Arguments
You can also send arguments with
the key = value syntax.
This way the order of the arguments does not matter.
Example
Output:
False
Example
Output:
True
Check if any item in a set is True:
myset = {0, 1, 0}
x = any(myset)
Output:
True
Check if any item in a dictionary is True:
mylist = []
x = any(mylist)
Output:
True
Python ascii() Function
Output:
My name is St\xe5
Parameter Values
Parameter Description
object An object, like String, List, Tuple, Dictionary etc.
Python bin() Function
Syntax:
bin(n)
Parameter Values
Parameter Description
n Required. An integer
Example
Output
0b100100
Python bool() function
Parameter Description
object Any object, like String, List, Number etc.
Syntax
bool(object)
Example
Return the boolean value of 1:
x = bool(1)
Output:
True
Python chr() Function
Syntax
chr(number)
Parameter Values
Parameter Description
number An integer representing a valid Unicode code point
x = chr(97)
print(x)
Output:
A
---------------------------------------------------
x = chr(65)
print(x)
Output:
A
Python complex() Function
Syntax
complex(real, imaginary)
Parameter Values
Parameter Description
x = complex(3, 5)
Python dict() Function
Syntax
dict(keyword arguments)
Parameter Values
Parameter Description
Output:
{'name': 'John', 'age': 36, 'country': 'Norway'}
Python divmod() Function
Syntax
divmod(dividend, divisor)
Parameter Values
Parameter Description
dividend A Number. The number you want to divide
divisor A Number. The number you want to divide with
Example
x = divmod(5, 2)
Output:
(2, 1)
Python eval() Function
Parameter Values
Parameter Description
expression A String, that will be evaluated as Python code
globals Optional. A dictionary containing global parameters
locals Optional. A dictionary containing local parameters
Example
x = 'print(55)'
eval(x)
Output:
55
Python float() Function
Parameter Values
Parameter Description
value A number or a string that can be converted into a floating
point number
Example
x = float(3)
Output:
3.0
Python format() Function
Syntax
format(value, format)
x = format(0.5, '%')
Output
50.000000%
x = format(255, 'x')
Output:
ff
Parameter Description
value A value of any format
format The format you want to format the value into.
Legal values:
'<' - Left aligns the result (within the available space)
'>' - Right aligns the result (within the available space)
'^' - Center aligns the result (within the available space)
'=' - Places the sign to the left most position
'+' - Use a plus sign to indicate if the result is positive or negative
'-' - Use a minus sign for negative values only
' ' - Use a leading space for positive numbers
',' - Use a comma as a thousand separator
'_' - Use a underscore as a thousand separator
'b' - Binary format
'c' - Converts the value into the corresponding unicode character
'd' - Decimal format
'e' - Scientific format, with a lower case e
'E' - Scientific format, with an upper case E
'f' - Fix point number format
'F' - Fix point number format, upper case
'g' - General format
'G' - General format (using a upper case E for scientific notations)
'o' - Octal format
'x' - Hex format, lower case
'X' - Hex format, upper case
'n' - Number format
'%' - Percentage format
Python help() function
help(list)
help(print)
Python hex() Function
Syntax
hex(number)
Parameter Values
Parameter Description
number An Integer
Example
x = hex(255)
Python input() Function
Syntax
input(prompt)
Parameter Values
Parameter Description
prompt A String, representing a default message before the input.
Example
Output:
Enter your name: mili
Hello, mili