Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
394 views

Variable Function: Python Keywords and Identifiers

Python keywords are reserved words that cannot be used as variable or function names. There are 33 keywords in Python 3.7 like False, None, True, and, as, assert, etc. Identifiers are names given to variables, functions, classes, etc. They can contain letters, numbers, and underscores but cannot start with a number. Comments are used to explain the code and are preceded by # or enclosed in triple quotes. Variables are containers that store values which can be changed, while constants are containers that store values which cannot be changed.

Uploaded by

pawarcp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
394 views

Variable Function: Python Keywords and Identifiers

Python keywords are reserved words that cannot be used as variable or function names. There are 33 keywords in Python 3.7 like False, None, True, and, as, assert, etc. Identifiers are names given to variables, functions, classes, etc. They can contain letters, numbers, and underscores but cannot start with a number. Comments are used to explain the code and are preceded by # or enclosed in triple quotes. Variables are containers that store values which can be changed, while constants are containers that store values which cannot be changed.

Uploaded by

pawarcp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

Python (Progrmiz.

com)

Python Keywords and Identifiers


In this tutorial, you will learn about keywords (reserved words in Python) and identifiers (names
given to variables, functions, etc.).

Python Keywords
Keywords are the reserved words in Python.
We cannot use a keyword as a variable name, function name or any other identifier. They are used to define
the syntax and structure of the Python language.
In Python, keywords are case sensitive.
There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.
All the keywords except True, False and None are in lowercase and they must be written as they are. The list
of all the keywords is given below.
False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

asser
del global not with
t

asyn
elif if or yield
c
Looking at all the keywords at once and trying to figure out what they mean might be overwhelming.
If you want to have an overview, here is the complete list of all the keywords with examples.

Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to
9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all are valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
3. Keywords cannot be used as identifiers.
global = 1
Output
File "<interactive input>", line 1
global = 1
^
SyntaxError: invalid syntax
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier. a@ = 0
Output
File "<interactive input>", line 1 a@ = 0
^
SyntaxError: invalid syntax
5. An identifier can be of any length.

Things to Remember
Python is a case-sensitive language. This means, Variable and variable are not the same.
Always give the identifiers a name that makes sense. While c = 10 is a valid name, writing count = 10 would
make more sense, and it would be easier to figure out what it represents when you look at your code after a
long gap.
Multiple words can be separated using an underscore, like this_is_a_long_variable.

Python Statement
Instructions that a Python interpreter can execute are called statements. For example, a = 1 is an assignment
statement. if statement, for statement, while statement, etc. are other kinds of statements which will be
discussed later.
Multi-line statement
In Python, the end of a statement is marked by a newline character. But we can make a statement extend
over multiple lines with the line continuation character (\). For example:
a=1+2+3+\
4+5+6+\
7+8+9
This is an explicit line continuation. In Python, line continuation is implied inside parentheses ( ),
brackets [ ], and braces { }. For instance, we can implement the above multi-line statement as:
a = (1 + 2 + 3 +
4+5+6+
7 + 8 + 9)
Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and { }.
For example:
colors = ['red', 'blue', 'green']
We can also put multiple statements in a single line using semicolons, as follows:
a = 1; b = 2; c = 3

Python Indentation
Most of the programming languages like C, C++, and Java use braces { } to define a block of code. Python,
however, uses indentation.
A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line.
The amount of indentation is up to you, but it must be consistent throughout that block.
Generally, four whitespaces are used for indentation and are preferred over tabs. Here is an example.
for i in range(1,11):
print(i)
if i == 5:
break
The enforcement of indentation in Python makes the code look neat and clean. This results in Python
programs that look similar and consistent.
Indentation can be ignored in line continuation, but it's always a good idea to indent. It makes the code more
readable. For example:
if True:
print('Hello')
a=5
and
if True: print('Hello'); a = 5
both are valid and do the same thing, but the former style is clearer.
Incorrect indentation will result in IndentationError.

Python Comments
Comments are very important while writing a program. They describe what is going on inside a program, so
that a person looking at the source code does not have a hard time figuring it out.
You might forget the key details of the program you just wrote in a month's time. So taking the time to
explain these concepts in the form of comments is always fruitful.
In Python, we use the hash (#) symbol to start writing a comment.
It extends up to the newline character. Comments are for programmers to better understand a program.
Python Interpreter ignores comments.
#This is a comment
#print out Hello
print('Hello')

Multi-line comments
We can have comments that extend up to multiple lines. One way is to use the hash(#) symbol at the
beginning of each line. For example:
#This is a long comment
#and it extends
#to multiple lines
Another way of doing this is to use triple quotes, either ''' or """.
These triple quotes are generally used for multi-line strings. But they can be used as a multi-line comment as
well. Unless they are not docstrings, they do not generate any extra code.
"""This is also a
perfect example of
multi-line comments"""
To learn more about comments, visit Python Comments.

Docstrings in Python
A docstring is short for documentation string.
Python docstrings (documentation strings) are the string literals that appear right after the definition of a
function, method, class, or module.
Triple quotes are used while writing docstrings. For example:
def double(num):
"""Function to double the value"""
return 2*num
Docstrings appear right after the definition of a function, class, or a module. This separates docstrings from
multiline comments using triple quotes.
The docstrings are associated with the object as their __doc__ attribute.
So, we can access the docstrings of the above function with the following lines of code:
def double(num):
"""Function to double the value"""
return 2*num
print(double.__doc__)
Output
Function to double the value
To learn more about docstrings in Python, visit Python Docstrings.

Python Variables
A variable is a named location used to store data in the memory. It is helpful to think of variables as a
container that holds data that can be changed later in the program. For example,
number = 10
Here, we have created a variable named number. We have assigned the value 10 to the variable.
You can think of variables as a bag to store books in it and that book can be replaced at any time.
number = 10
number = 1.1
Initially, the value of number was 10. Later, it was changed to 1.1.
Note: In Python, we don't actually assign values to the variables. Instead, Python gives the reference of the
object(value) to the variable.
Assigning values to Variables in Python
As you can see from the above example, you can use the assignment operator = to assign a value to a
variable.
Example 1: Declaring and assigning value to a variable
website = "apple.com"
print(website)
Output
apple.com
In the above program, we assigned a value apple.com to the variable website. Then, we printed out
the value assigned to website i.e. apple.com
Note: Python is a type-inferred language, so you don't have to explicitly define the variable type. It
automatically knows that apple.com is a string and declares the website variable as a string.

Example 2: Changing the value of a variable


website = "apple.com"
print(website)

# assigning a new value to website


website = "programiz.com"

print(website)
Output
apple.com
programiz.com
In the above program, we have assigned apple.com to the website variable initially. Then, the value
is changed to programiz.com.

Example 3: Assigning multiple values to multiple variables


a, b, c = 5, 3.2, "Hello"

print (a) print (b) print (c)


If we want to assign the same value to multiple variables at once, we can do this as:
x = y = z = "same"

print (x) print (y) print (z)


The second program assigns the same string to all the three variables x, y and z.

Constants
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as
containers that hold information which cannot be changed later.
You can think of constants as a bag to store some books which cannot be replaced once placed inside the
bag.

Assigning value to constant in Python


In Python, constants are usually declared and assigned in a module. Here, the module is a new file
containing variables, functions, etc which is imported to the main file. Inside the module, constants are
written in all capital letters and underscores separating the words.
Example 3: Declaring and assigning value to a constant
Create a constant.py:
PI = 3.14
GRAVITY = 9.8
Create a main.py:
import constant
print(constant.PI)
print(constant.GRAVITY)
Output
3.14 9.8
In the above program, we create a constant.py module file. Then, we assign the constant value
to PI and GRAVITY. After that, we create a main.py file and import the constant module. Finally, we print
the constant value.
Note: In reality, we don't use constants in Python. Naming them in all capital letters is a convention
to separate them from variables, however, it does not actually prevent reassignment.

Rules and Naming Convention for Variables and constants


1. Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase
(A to Z) or digits (0 to 9) or an underscore (_). For example:
2. snake_case
3. MACRO_CASE
4. camelCase
CapWords
5. Create a name that makes sense. For example, vowel makes more sense than v.
6. If you want to create a variable name having two words, use underscore to separate them. For
example:
7. my_name
current_salary
8. Use capital letters possible to declare a constant. For example:
9. PI
10. G
11. MASS
12. SPEED_OF_LIGHT
TEMP
13. Never use special symbols like !, @, #, $, %, etc.
14. Don't start a variable name with a digit.

Literals
Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as
follows:
Numeric Literals
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical
types: Integer, Float, and Complex.
Example 4: How to use Numeric literals in Python?
a = 0b1010 #Binary Literals
b = 100 #Decimal Literal
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal

#Float Literal
float_1 = 10.5
float_2 = 1.5e2

#Complex Literal
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Output
10 100 200 300
10.5 150.0
3.14j 3.14 0.0
In the above program,
 We assigned integer literals into different variables. Here, a is binary literal, b is a decimal literal, c is
an octal literal and d is a hexadecimal literal.
 When we print the variables, all the literals are converted into decimal values.
 10.5 and 1.5e2 are floating-point literals. 1.5e2 is expressed with exponential and is equivalent to 1.5
* 102.
 We assigned a complex literal i.e 3.14j in variable x. Then we use imaginary literal (x.imag)
and real literal (x.real) to create imaginary and real parts of complex numbers.
To learn more about Numeric Literals, refer to Python Numbers.

String literals
A string literal is a sequence of characters surrounded by quotes. We can use both single, double, or
triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes.
Example 7: How to use string literals in Python?
strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"

print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Output
This is Python
C
This is a multiline string with more than one line code.
Ünicöde
raw \n string
In the above program, This is Python is a string literal and C is a character literal.
The value in triple-quotes """ assigned to the multiline_str is a multi-line string literal.
The string u"\u00dcnic\u00f6de" is a Unicode literal which supports characters other than English. In this
case, \u00dc represents Ü and \u00f6 represents ö.
r"raw \n string" is a raw string literal.

Boolean literals
A Boolean literal can have any of the two values: True or False.
Example 8: How to use boolean literals in Python?
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Output
x is True y is False a: 5 b: 10
In the above program, we use boolean literal True and False. In Python, True represents the value
as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value
of y is False because 1 is not equal to False.
Similarly, we can use the True and False in numeric expressions as the value. The value of a is 5 because we
add True which has a value of 1 with 4. Similarly, b is 10 because we add the False having value
of 0 with 10.

Special literals
Python contains one special literal i.e. None. We use it to specify that the field has not been created.
Example 9: How to use special literals in Python?
drink = "Available"
food = None

def menu(x):
if x == drink:
print(drink)
else:
print(food)

menu(drink)
menu(food)
Output
Available
None
In the above program, we define a menu function. Inside menu, when we set the argument as drink then, it
displays Available. And, when the argument is food, it displays None.

Literal Collections
There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.
Example 10: How to use literals collections in Python?
fruits = ["apple", "mango", "orange"] #list
numbers = (1, 2, 3) #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'} #set

print(fruits) print(numbers) print(alphabets) print(vowels)


Output
['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}
In the above program, we created a list of fruits, a tuple of numbers, a dictionary dict having values with
keys designated to each value and a set of vowels.

Data types in Python


Every value in Python has a datatype. Since everything is an object in Python programming, data types are
actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the important types are listed below.

Python Numbers
Integers, floating point numbers and complex numbers fall under Python numbers category. They are
defined as int, float and complex classes in Python.
We can use the type() function to know which class a variable or a value belongs to. Similarly,
the isinstance() function is used to check if an object belongs to a particular class.
a = 5 print(a, "is of type", type(a))
a = 2.0 print(a, "is of type", type(a))
a = 1+2j print(a, "is complex number?", isinstance(1+2j,complex))
Output: 5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is complex number? True
Integers can be of any length, it is only limited by the memory available.
A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by
decimal points. 1 is an integer, 1.0 is a floating-point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part. Here
are some examples.
>>> a = 1234567890123456789
>>> a
1234567890123456789
>>> b = 0.1234567890123456789
>>> b
0.12345678901234568
>>> c = 1+2j
>>> c
(1+2j)
Notice that the float variable b got truncated.

Python List
List is an ordered sequence of items. It is one of the most used datatype 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']
We can use the slicing operator [ ] to extract an item or a range of items from a list. The index starts from 0
in 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:])
Output
a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
Lists are mutable, meaning, the value of elements of a list can be altered.
a = [1, 2, 3]
a[2] = 4
print(a)
Output
[1, 2, 4]

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+3j)
We can use the slicing operator [] to extract items but we cannot change its value.
t = (5,'program', 1+3j)
# t[1] = 'program'
print("t[1] = ", t[1])

# t[0:3] = (5, 'program', (1+3j))


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

# Generates error
# Tuples are immutable
t[0] = 10
Output
t[1] = program
t[0:3] = (5, 'program', (1+3j))
Traceback (most recent call last):
File "test.py", line 11, in <module>
t[0] = 10
TypeError: 'tuple' object does not support item assignment

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 """.
s = "This is a string" print(s)
s = '''A multiline
string''' print(s)
Output
This is a string A multiline string
Just like a list and tuple, the slicing operator [ ] can be used with strings. Strings, however, are
immutable.
s = 'Hello world!'

# s[4] = 'o' print("s[4] = ", s[4])

# s[6:11] = 'world' print("s[6:11] = ", s[6:11])

# Generates error
# Strings are immutable in Python
s[5] ='d'
Output
s[4] = o s[6:11] = world
Traceback (most recent call last):
File "<string>", line 11, in <module>
TypeError: 'str' object does not support item assignment

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) # printing set variable
print(type(a)) # data type of variable a
Output
a = {1, 2, 3, 4, 5}
<class 'set'>
We can perform set operations like union, intersection on two sets. Sets have unique values. They eliminate
duplicates.
a = {1,2,2,3,3,3} print(a)
Output
{1, 2, 3}
Since, set are unordered collection, indexing has no meaning. Hence, the slicing operator [] does not work.
>>> a = {1,2,3}
>>> a[1]
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
TypeError: 'set' object does not support indexing

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}
>>> type(d)
<class 'dict'>
We use key to retrieve the respective value. But not the other way around.
d = {1:'value','key':2}
print(type(d))
print("d[1] = ", d[1]);
print("d['key'] = ", d['key']);
# Generates error
print("d[2] = ", d[2]);
Output
<class 'dict'>
d[1] = value
d['key'] = 2
Traceback (most recent call last):
File "<string>", line 9, in <module>
KeyError: 2

Conversion between data types


We can convert between different data types by using different type conversion functions
like int(), float(), str(), etc.
>>> float(5)
5.0
Conversion from float to int will truncate the value (make it closer to zero).
>>> int(10.6)
10
>>> int(-10.6)
-10
Conversion to and from string must contain compatible values.
>>> float('2.5')
2.5
>>> str(25)
'25'
>>> int('1p')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1p'
We can even convert one sequence to another.
>>> set([1,2,3])
{1, 2, 3}
>>> tuple({5,6,7})
(5, 6, 7)
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
To convert to dictionary, each element must be a pair:
>>> dict([[1,2],[3,4]])
{1: 2, 3: 4}
>>> dict([(3,26),(4,44)])
{3: 26, 4: 44}

Type Conversion
The process of converting the value of one data type (integer, string, float, etc.) to another data type is
called type conversion. Python has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion

Implicit Type Conversion


In Implicit type conversion, Python automatically converts one data type to another data type. This
process doesn't need any user involvement.
Let's see an example where Python promotes the conversion of the lower data type (integer) to the higher
data type (float) to avoid data loss.
Example 1: Converting integer to float
num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
When we run the above program, the output will be:
datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23


datatype of num_new: <class 'float'>
In the above program,
 We add two variables num_int and num_flo, storing the value in num_new.
 We will look at the data type of all three objects respectively.
 In the output, we can see the data type of num_int is an integer while the data type of num_flo is
a float.
 Also, we can see the num_new has a float data type because Python always converts smaller data
types to larger data types to avoid the loss of data.

Now, let's try adding a string and an integer, and see how Python deals with it.
Example 2: Addition of string(higher) data type and integer(lower) datatype
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str:",type(num_str))
print(num_int+num_str)
When we run the above program, the output will be:
Data type of num_int: <class 'int'>
Data type of num_str: <class 'str'>

Traceback (most recent call last):


File "python", line 7, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In the above program,
 We add two variables num_int and num_str.
 As we can see from the output, we got TypeError. Python is not able to use Implicit Conversion in
such conditions.
 However, Python has a solution for these types of situations which is known as Explicit Conversion.

Explicit Type Conversion


In Explicit Type Conversion, users convert the data type of an object to required data type. We use the
predefined functions like int(), float(), str(), etc to perform explicit type conversion.
This type of conversion is also called typecasting because the user casts (changes) the data type of the
objects.
Syntax :
<required_datatype>(expression)
Typecasting can be done by assigning the required data type function to the expression.

Example 3: Addition of string and integer using explicit conversion


num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)


print("Data type of the sum:",type(num_sum))
When we run the above program, the output will be:
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579


Data type of the sum: <class 'int'>
In the above program,
 We add num_str and num_int variable.
 We converted num_str from string(higher) to integer(lower) type using int() function to perform the
addition.
 After converting num_str to an integer value, Python is able to add these two variables.
 We got the num_sum value and data type to be an integer.

Key Points to Remember


1. Type Conversion is the conversion of object from one data type to another data type.
2. Implicit Type Conversion is automatically performed by the Python interpreter.
3. Python avoids the loss of data in Implicit Type Conversion.
4. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using
predefined functions by the user.
5. In Type Casting, loss of data may occur as we enforce the object to a specific data type.

Python provides numerous built-in functions that are readily available to us at the Python prompt.
Some of the functions like input() and print() are widely used for standard input and output operations
respectively. Let us see the output section first.

Python Output Using print() function


We use the print() function to output data to the standard output device (screen). We can also output data to
a file, but this will be discussed later.
An example of its use is given below.
print('This sentence is output to the screen')
Output
This sentence is output to the screen
Another example is given below:
a=5
print('The value of a is', a)
Output
The value of a is 5
In the second print() statement, we can notice that space was added between the string and the value of
variable a. This is by default, but we can change it.
The actual syntax of the print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Here, objects is the value(s) to be printed.
The sep separator is used between the values. It defaults into a space character.
After all values are printed, end is printed. It defaults into a new line.
The file is the object where the values are printed and its default value is sys.stdout (screen). Here is an
example to illustrate this.
print(1, 2, 3, 4) print(1, 2, 3, 4, sep='*') print(1, 2, 3, 4, sep='#', end='&')
Output
1234 1*2*3*4 1#2#3#4&

Output formatting
Sometimes we would like to format our output to make it look attractive. This can be done by using
the str.format() method. This method is visible to any string object.
>>> x = 5; y = 10
>>> print('The value of x is {} and y is {}'.format(x,y))
The value of x is 5 and y is 10
Here, the curly braces {} are used as placeholders. We can specify the order in which they are printed by
using numbers (tuple index).
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))
Output
I love bread and butter I love butter and bread
We can even use keyword arguments to format the string.
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John'))
Hello John, Goodmorning
We can also format strings like the old sprintf() style used in C programming language. We use
the % operator to accomplish this.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457

Python Input
Up until now, our programs were static. The value of variables was defined or hard coded into the
source code.
To allow flexibility, we might want to take the input from the user. In Python, we have the input() function
to allow this. The syntax for input() is: input([prompt])
where prompt is the string we wish to display on the screen. It is optional.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can
use int() or float() functions.
>>> int('10')
10
>>> float('10')
10.0
This same operation can be performed using the eval() function. But eval takes it further. It can evaluate
even expressions, provided the input is a string
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5

Python Import
When our program grows bigger, it is a good idea to break it into different modules.
A module is a file containing Python definitions and statements. Python modules have a filename and end
with the extension .py.
Definitions inside a module can be imported to another module or the interactive interpreter in Python. We
use the import keyword to do this.
For example, we can import the math module by typing the following line:
import math
We can use the module in the following ways:
import math
print(math.pi)
Output
3.141592653589793
Now all the definitions inside math module are available in our scope. We can also import some
specific attributes and functions only, using the from keyword. For example:
>>> from math import pi
>>> pi
3.141592653589793
While importing a module, Python looks at several places defined in sys.path. It is a list of directory
locations.
>>> import sys
>>> sys.path
['',
'C:\\Python33\\Lib\\idlelib',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']

What are operators in python?


Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the
operator operates on is called the operand.
For example:
>>> 2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication,
etc.
Operato
Meaning Example
r

+ Add two operands or unary plus x + y+ 2

- Subtract right operand from the left or unary minus x - y- 2

* Multiply two operands x*y

/ Divide left operand by the right one (always results into float) x/y

x % y (remainder o
% Modulus - remainder of the division of left operand by the right
x/y)

Floor division - division that results into whole number adjusted to the
// x // y
left in the number line

x**y (x to the pow


** Exponent - left operand raised to the power of right
y)
Example 1: Arithmetic operators in Python
x = 15 y=4
print('x + y =',x+y) # Output: x + y = 19

print('x - y =',x-y) # Output: x - y = 11

print('x * y =',x*y) # Output: x * y = 60

print('x / y =',x/y) # Output: x / y = 3.75

print('x // y =',x//y) # Output: x // y = 3

print('x ** y =',x**y) # Output: x ** y = 50625


Output
x + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625
Comparison operators
Comparison operators are used to compare values. It returns either True or False according to the condition.

Operator Meaning Examp

> Greater than - True if left operand is greater than the right x>y

< Less than - True if left operand is less than the right x<y

== Equal to - True if both operands are equal x == y

!= Not equal to - True if operands are not equal x != y

>= Greater than or equal to - True if left operand is greater than or equal to the right x >= y

<= Less than or equal to - True if left operand is less than or equal to the right x <= y
Example 2: Comparison operators in Python
x = 10 y = 12

print('x > y is',x>y) # Output: x > y is False

print('x < y is',x<y) # Output: x < y is True

print('x == y is',x==y) # Output: x == y is False

print('x != y is',x!=y) # Output: x != y is True

print('x >= y is',x>=y) # Output: x >= y is False

print('x <= y is',x<=y) # Output: x <= y is True


Output
x > y is False x < y is True x == y is False x != y is True
x >= y is False x <= y is True

Logical operators
Logical operators are the and, or, not operators.
Operator Meaning Example

and True if both the operands are true x and y

or True if either of the operands is true x or y

not True if operand is false (complements the operand) not x


Example 3: Logical Operators in Python
x = True y = False
print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x)
Output
x and y is False x or y is True not x is False
Here is the truth table for these operators.

Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit,
hence the name.
For example, 2 is 10 in binary and 7 is 111.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Operator Meaning Example

& Bitwise AND x & y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x >> 2 = 2 (0000 0010)

<< Bitwise left shift x << 2 = 40 (0010 1000)

Assignment operators
Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like a += 5 that adds to the variable and later assigns the
same. It is equivalent to a = a + 5.
Operator Example Equivalent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Special operators
Python language offers some special types of operators like the identity operator or the membership
operator. They are described below with examples.
Identity operators
is and is not are the identity operators in Python. They are used to check if two values (or variables) are
located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Operato
Meaning Example
r

is True if the operands are identical (refer to the same object) x is True

True if the operands are not identical (do not refer to the same
is not x is not True
object)
Example 4: Identity operators in Python
x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [1,2,3] y3 = [1,2,3]

print(x1 is not y1) # Output: False

print(x2 is y2) # Output: True

print(x3 is y3) # Output: False


Output
False True False
Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical.
Same is the case with x2 and y2 (strings).
But x3 and y3 are lists. They are equal but not identical. It is because the interpreter locates them separately
in memory although they are equal.

Membership operators
in and not in are the membership operators in Python. They are used to test whether a value or variable is
found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Operator Meaning Example

in True if value/variable is found in the sequence 5 in x

not in True if value/variable is not found in the sequence 5 not in x


Example #5: Membership operators in Python
x = 'Hello world' y = {1:'a',2:'b'}

print('H' in x) # Output: True print('hello' not in x) # Output: True

print(1 in y) # Output: True print('a' in y) # Output: False

Output
True True True False
Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive). Similarly, 1 is key
and 'a' is the value in dictionary y. Hence, 'a' in y returns False.

What is Name in Python?


If you have ever read 'The Zen of Python' (type import this in the Python interpreter), the last line
states, Namespaces are one honking great idea -- let's do more of those! So what are these mysterious
namespaces? Let us first look at what name is.
Name (also called identifier) is simply a name given to objects. Everything in Python is an object. Name is a
way to access the underlying object.
For example, when we do the assignment a = 2, 2 is an object stored in memory and a is the name we
associate it with. We can get the address (in RAM) of some object through the built-in function id(). Let's
look at how to use it.
# Note: You may get different values for the id

a=2 print('id(2) =', id(2)) print('id(a) =', id(a))


Output
id(2) = 9302208 id(a) = 9302208
Here, both refer to the same object 2, so they have the same id(). Let's make things a little more interesting.
# Note: You may get different values for the id

a=2 print('id(a) =', id(a)) a = a+1 print('id(a) =', id(a))

print('id(3) =', id(3))

b=2
print('id(b) =', id(b)) print('id(2) =', id(2))
Output
id(a) = 9302208 id(a) = 9302240 id(3) = 9302240
id(b) = 9302208 id(2) = 9302208
What is happening in the above sequence of steps? Let's use a diagram to explain this:

Memory diagram of variables in Python


Initially, an object 2 is created and the name a is associated with it, when we do a = a+1, a new object 3 is
created and now a is associated with this object.
Note that id(a) and id(3) have the same values.
Furthermore, when b = 2 is executed, the new name b gets associated with the previous object 2.
This is efficient as Python does not have to create a new duplicate object. This dynamic nature of name
binding makes Python powerful; a name could refer to any type of object.
>>> a = 5
>>> a = 'Hello World!'
>>> a = [1,2,3]
All these are valid and a will refer to three different types of objects in different instances. Functions are
objects too, so a name can refer to them as well.
def printHello():
print("Hello")
a = printHello a() Output Hello
The same name a can refer to a function and we can call the function using this name.

What is a Namespace in Python?


Now that we understand what names are, we can move on to the concept of namespaces.
To simply put it, a namespace is a collection of names.
In Python, you can imagine a namespace as a mapping of every name you have defined to corresponding
objects.
Different namespaces can co-exist at a given time but are completely isolated.
A namespace containing all the built-in names is created when we start the Python interpreter and exists as
long as the interpreter runs.
This is the reason that built-in functions like id(), print() etc. are always available to us from any part of the
program. Each module creates its own global namespace.
These different namespaces are isolated. Hence, the same name that may exist in different modules do not
collide.
Modules can have various functions and classes. A local namespace is created when a function is called,
which has all the names defined in it. Similar, is the case with class. Following diagram may help to clarify
this concept.

A diagram of different namespaces in python


Python Variable Scope
Although there are various unique namespaces defined, we may not be able to access all of them from every
part of the program. The concept of scope comes into play.
A scope is the portion of a program from where a namespace can be accessed directly without any prefix.
At any given moment, there are at least three nested scopes.
1. Scope of the current function which has local names
2. Scope of the module which has global names
3. Outermost scope which has built-in names
When a reference is made inside a function, the name is searched in the local namespace, then in the global
namespace and finally in the built-in namespace.
If there is a function inside another function, a new scope is nested inside the local scope.

Example of Scope and Namespace in Python


def outer_function():
b = 20
def inner_func():
c = 30

a = 10
Here, the variable a is in the global namespace. Variable b is in the local namespace
of outer_function() and c is in the nested local namespace of inner_function().
When we are in inner_function(), c is local to us, b is nonlocal and a is global. We can read as well as assign
new values to c but can only read b and a from inner_function().
If we try to assign as a value to b, a new variable b is created in the local namespace which is different than
the nonlocal b. The same thing happens when we assign a value to a.
However, if we declare a as global, all the reference and assignment go to the global a. Similarly, if we want
to rebind the variable b, it must be declared as nonlocal. The following example will further clarify this.
def outer_function():
a = 20
def inner_function():
a = 30
print('a =', a)
inner_function()
print('a =', a)
a = 10
outer_function()
print('a =', a)
As you can see, the output of this program is
a = 30 a = 20 a = 10
In this program, three different variables a are defined in separate namespaces and accessed accordingly.
While in the following program,
def outer_function():
global a
a = 20
def inner_function():
global a
a = 30
print('a =', a)
inner_function()
print('a =', a)
a = 10
outer_function()
print('a =', a)
The output of the program is.
a = 30 a = 30 a = 30

What is if...else statement in Python?


Decision making is required when we want to execute a code only if a certain condition is satisfied.
The if…elif…else statement is used in Python for decision making.
Python if Statement Syntax
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the test expression
is True.
If the test expression is False, the statement(s) is not executed.
In Python, the body of the if statement is indicated by the indentation. The body starts with an indentation
and the first unindented line marks the end.
Python interprets non-zero values as True. None and 0 are interpreted as False.
Python if Statement Flowchart

Example: Python if Statement


# If the number is positive, we print an appropriate message

num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
When you run the program, the output will be:
3 is a positive number
This is always printed
This is also always printed.
In the above example, num > 0 is the test expression.
The body of if is executed only if this evaluates to True.
When the variable num is equal to 3, test expression is true and statements inside the body of if are executed.
If the variable num is equal to -1, test expression is false and statements inside the body of if are skipped.
The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of the test
expression.

Python if...else Statement


Syntax of if...else
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute the body of if only when the test condition
is True.
If the condition is False, the body of else is executed. Indentation is used to separate the blocks.
Python if..else Flowchart

Example of if...else
# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well.


# num = -5
# num = 0

if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output
Positive or Zero
In the above example, when num is equal to 3, the test expression is true and the body of if is executed and
the body of else is skipped.
If num is equal to -5, the test expression is false and the body of else is executed and the body of if is
skipped.
If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped.

Python if...elif...else Statement


Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, the body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.
Flowchart of if...elif...else

Example of if...elif...else
'''In this program,
we check if the number is positive or
negative or zero and
display an appropriate message'''

num = 3.4

# Try these two variations as well:


# num = 0
# num = -4.5

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
When variable num is positive, Positive number is printed.
If num is equal to 0, Zero is printed.
If num is negative, Negative number is printed.
Python Nested if statements
We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in
computer programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure out
the level of nesting. They can get confusing, so they must be avoided unless necessary.
Python Nested if Example
'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output 1
Enter a number: 5 Positive number
Output 2
Enter a number: -1 Negative number
Output 3
Enter a number: 0 Zero

What is for loop in Python?


The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating
over a sequence is called traversal.
Syntax of for Loop
for val in sequence:
Body of for
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest
of the code using indentation.
Flowchart of for Loop

Example: Python for Loop


# Program to find the sum of all numbers stored in a list

# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

print("The sum is", sum)

When you run the program, the output will be:


The sum is 48

The range() function


We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9
(10 numbers).
We can also define the start, stop and step size as range(start, stop,step_size). step_size defaults to 1 if not
provided.
The range object is "lazy" in a sense because it doesn't generate every number that it "contains" when we
create it. However, it is not an iterator since it supports in, len and __getitem__ operations.
This function does not store all the values in memory; it would be inefficient. So it remembers the start, stop,
step size and generates the next number on the go.
To force this function to output all the items, we can use the function list().
The following example will clarify this.
print(range(10)) print(list(range(10))) print(list(range(2, 8))) print(list(range(2, 20, 3)))
Output
range(0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 3, 4, 5, 6, 7] [2, 5, 8, 11, 14, 17]
We can use the range() function in for loops to iterate through a sequence of numbers. It can be combined
with the len() function to iterate through a sequence using indexing. Here is an example.
# Program to iterate through a list using indexing

genre = ['pop', 'rock', 'jazz']

# iterate over the list using index


for i in range(len(genre)):
print("I like", genre[i])
Output
I like pop I like rock I like jazz

for loop with else


A for loop can have an optional else block as well. The else part is executed if the items in the sequence
used in for loop exhausts.
The break keyword can be used to stop a for loop. In such cases, the else part is ignored.
Hence, a for loop's else part runs if no break occurs.
Here is an example to illustrate this.
digits = [0, 1, 5]

for i in digits:
print(i)
else:
print("No items left.")
When you run the program, the output will be:
0 1 5
No items left.
Here, the for loop prints items of the list until the loop exhausts. When the for loop exhausts, it executes the
block of code in the else and prints No items left.
This for...else statement can be used with the break keyword to run the else block only when
the break keyword was not executed. Let's take an example:
# program to display student's marks from record
student_name = 'Soyuj'

marks = {'James': 90, 'Jules': 55, 'Arthur': 77}

for student in marks:


if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')
Output
No entry with that name found.

What is while loop in Python?


The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is
true.
We generally use this loop when we don't know the number of times to iterate beforehand.
Syntax of while Loop in Python
while test_expression:
Body of while
In the while loop, test expression is checked first. The body of the loop is entered only if
the test_expression evaluates to True. After one iteration, the test expression is checked again. This process
continues until the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
Flowchart of while Loop

# Program to add natural


# numbers up to
# sum = 1+2+3+...+n

# To take input from the user,


# n = int(input("Enter n: "))

n = 10
# initialize sum and counter
sum = 0
i=1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)
When you run the program, the output will be:
Enter n: 10
The sum is 55
In the above program, the test expression will be True as long as our counter variable i is less than or equal
to n (10 in our program).
We need to increase the value of the counter variable in the body of the loop. This is very important (and
mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop).
Finally, the result is displayed.

While loop with else


Same as with for loops, while loops can also have an optional else block.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a
while loop's else part runs if no break occurs and the condition is false.
Here is an example to illustrate this.
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:


print("Inside loop")
counter = counter + 1
else:
print("Inside else")
Output
Inside loop Inside loop Inside loop Inside else
Here, we use a counter variable to print the string Inside loop three times.
On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.

What is the use of break and continue in Python?


In Python, break and continue statements can alter the flow of a normal loop.
Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the
current iteration or even the whole loop without checking test expression.
The break and continue statements are used in these cases.

Python break statement


The break statement terminates the loop containing it. Control of the program flows to the statement
immediately after the body of the loop.
If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate
the innermost loop.
Syntax of break
break
Flowchart of break
The working of break statement in for loop and while loop is shown below.

Example: Python break


# Use of break statement inside the loop

for val in "string":


if val == "i":
break
print(val)

print("The end")
Output
S t r
The end
In this program, we iterate through the "string" sequence. We check if the letter is i, upon which we break
from the loop. Hence, we see in our output that all the letters up till i gets printed. After that, the loop
terminates.

Python continue statement


The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop
does not terminate but continues on with the next iteration.
Syntax of Continue
continue
Flowchart of continue 

The working of continue statement in for and while loop is shown below.

Example: Python continue


# Program to show the use of continue statement inside loops

for val in "string":


if val == "i":
continue
print(val)

print("The end")
Output
s t r n g
The end
This program is same as the above example except the break statement has been replaced with continue.
We continue with the loop, if the string is i, not executing the rest of the block. Hence, we see in our output
that all the letters except i gets printed.

What is pass statement in Python?


In Python programming, the pass statement is a null statement. The difference between a comment and
a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored.
However, nothing happens when the pass is executed. It results in no operation (NOP).
Syntax of pass
pass
We generally use it as a placeholder.
Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future.
They cannot have an empty body. The interpreter would give an error. So, we use the pass statement to
construct a body that does nothing.
Example: pass Statement
'''pass is just a placeholder for
functionality to be added later.'''
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass
We can do the same thing in an empty function or class as well.
def function(args):
pass
class Example:
pass

What is a 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.
Furthermore, it avoids repetition and makes the code reusable.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Above shown is a function definition that consists of the following components.
1. Keyword def that marks the start of the function header.
2. A function name to uniquely identify the function. Function naming follows the same rules of
writing identifiers in Python.
3. Parameters (arguments) through which we pass values to a function. They are optional.
4. A colon (:) to mark the end of the function header.
5. Optional documentation string (docstring) to describe what the function does.
6. One or more valid python statements that make up the function body. Statements must have the same
indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.
Example of a function
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
How to call a function in python?
Once we have defined a function, we can call it from another function, program or even the Python prompt.
To call a function we simply type the function name with appropriate parameters.
>>> greet('Paul')
Hello, Paul. Good morning!
Note: Try running the above code in the Python program with the function definition to see the output.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")

greet('Paul')

Docstrings
The first string after the function header is called the docstring and is short for documentation string. It is
briefly used to explain what a function does.
Although optional, documentation is a good programming practice. Unless you can remember what you had
for dinner last week, always document your code.
In the above example, we have a docstring immediately below the function header. We generally use triple
quotes so that docstring can extend up to multiple lines. This string is available to us as the __doc__ attribute
of the function.
For example:
Try running the following into the Python shell to see the output.
>>> print(greet.__doc__)

This function greets to


the person passed in as
a parameter
 
To learn more about docstrings in Python, visit Python Docstrings.

The return statement


The return statement is used to exit a function and go back to the place from where it was called.
Syntax of return
return [expression_list]
This statement can contain an expression that gets evaluated and the value is returned. If there is no
expression in the statement or the return statement itself is not present inside a function, then the function
will return the None object.
For example:
>>> print(greet("May"))
Hello, May. Good morning!
None
Here, None is the returned value since greet() directly prints the name and no return statement is used.

Example of return
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
print(absolute_value(2))
print(absolute_value(-4))
Output
2 4

How Function works in Python?


Scope and Lifetime of variables
Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables
defined inside a function are not visible from outside the function. Hence, they have a local scope.
The lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of
variables inside a function is as long as the function executes.
They are destroyed once we return from the function. Hence, a function does not remember the value of a
variable from its previous calls.
Here is an example to illustrate the scope of a variable inside a function.
def my_func():
x = 10
print("Value inside function:",x)

x = 20
my_func()
print("Value outside function:",x)
Output
Value inside function: 10 Value outside function: 20
Here, we can see that the value of x is 20 initially. Even though the function my_func() changed the value
of x to 10, it did not affect the value outside the function.
This is because the variable x inside the function is different (local to the function) from the one outside.
Although they have the same names, they are two different variables with different scopes.
On the other hand, variables outside of the function are visible from inside. They have a global scope.
We can read these values from inside the function but cannot change (write) them. In order to modify the
value of variables outside the function, they must be declared as global variables using the keyword global.

Types of Functions
Basically, we can divide functions into the following two types:
1. Built-in functions - Functions that are built into Python.
2. User-defined functions - Functions defined by the users themselves.

Functions Arguments
In the user-defined function topic, we learned about defining a function and calling it. Otherwise, the
function call will result in an error. Here is an example.
def greet(name, msg):
"""This function greets to
the person with the provided message"""
print("Hello", name + ', ' + msg)

greet("Monica", "Good morning!")


Output
Hello Monica, Good morning!
Here, the function greet() has two parameters.
Since we have called this function with two arguments, it runs smoothly and we do not get any error.
If we call it with a different number of arguments, the interpreter will show an error message. Below is a call
to this function with one and no arguments along with their respective error messages.
>>> greet("Monica") # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet() # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'

Variable Function Arguments


Up until now, functions had a fixed number of arguments. In Python, there are other ways to define a
function that can take variable number of arguments.
Three different forms of this type are described below.
Python Default Arguments
Function arguments can have default values in Python.
We can provide a default value to an argument by using the assignment operator (=). Here is an example.
def greet(name, msg="Good morning!"):
"""
This function greets to
the person with the
provided message.

If the message is not provided,


it defaults to "Good
morning!"
"""
print("Hello", name + ', ' + msg)

greet("Kate")
greet("Bruce", "How do you do?")
Output
Hello Kate, Good morning!
Hello Bruce, How do you do?
In this function, the parameter name does not have a default value and is required (mandatory) during a call.
On the other hand, the parameter msg has a default value of "Good morning!". So, it is optional during a
call. If a value is provided, it will overwrite the default value.
Any number of arguments in a function can have a default value. But once we have a default argument, all
the arguments to its right must also have default values.
This means to say, non-default arguments cannot follow default arguments. For example, if we had defined
the function header above as:
def greet(msg = "Good morning!", name):
We would get an error as:
SyntaxError: non-default argument follows default argument

Python Keyword Arguments


When we call a function with some values, these values get assigned to the arguments according to their
position.
For example, in the above function greet(), when we called it as greet("Bruce", "How do you do?"), the
value "Bruce" gets assigned to the argument name and similarly "How do you do?" to msg.
Python allows functions to be called using keyword arguments. When we call functions in this way, the
order (position) of the arguments can be changed. Following calls to the above function are all valid and
produce the same result.
# 2 keyword arguments
greet(name = "Bruce",msg = "How do you do?")
# 2 keyword arguments (out of order)
greet(msg = "How do you do?",name = "Bruce")

1 positional, 1 keyword argument


greet("Bruce", msg = "How do you do?")
As we can see, we can mix positional arguments with keyword arguments during a function call. But we
must keep in mind that keyword arguments must follow positional arguments.
Having a positional argument after keyword arguments will result in errors. For example, the function call as
follows:
greet(name="Bruce","How do you do?")
Will result in an error:
SyntaxError: non-keyword arg after keyword arg

Python Arbitrary Arguments


Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python
allows us to handle this kind of situation through function calls with an arbitrary number of arguments.
In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument.
Here is an example.
def greet(*names):
"""This function greets all
the person in the names tuple."""

# names is a tuple with arguments


for name in names:
print("Hello", name)

greet("Monica", "Luke", "Steve", "John")


Output
Hello Monica Hello Luke Hello Steve Hello John
Here, we have called the function with multiple arguments. These arguments get wrapped up into a tuple
before being passed into the function. Inside the function, we use a for loop to retrieve all the arguments
back.

What is recursion?
Recursion is the process of defining something in terms of itself.
A physical world example would be to place two parallel mirrors facing each other. Any object in between
them would be reflected recursively.

Python Recursive Function


In Python, we know that a function can call other functions. It is even possible for the function to call itself.
These types of construct are termed as recursive functions.
The following image shows the working of a recursive function called recurse.

Recursive Function in Python


Following is an example of a recursive function to find the factorial of an integer.
Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6
(denoted as 6!) is 1*2*3*4*5*6 = 720.
Example of a recursive function
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""

if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
Output
The factorial of 3 is 6
In the above example, factorial() is a recursive function as it calls itself.
When we call this function with a positive integer, it will recursively call itself by decreasing the number.
Each function multiplies the number with the factorial of the number below it until it is equal to one. This
recursive call can be explained in the following steps.
factorial(3) # 1st call with 3
3 * factorial(2) # 2nd call with 2
3 * 2 * factorial(1) # 3rd call with 1
3*2*1 # return from 3rd call as number=1
3*2 # return from 2nd call
6 # return from 1st call
Let's look at an image that shows a step-by-step process of what is going on:

Our recursion ends when the number reduces to 1. This is called the base condition.
Every recursive function must have a base condition that stops the recursion or else the function calls itself
infinitely.
The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack
overflows.
By default, the maximum depth of recursion is 1000. If the limit is crossed, it results in RecursionError.
Let's look at one such condition.
def recursor():
recursor()
recursor()
Output
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "<string>", line 2, in a
File "<string>", line 2, in a
File "<string>", line 2, in a
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

Advantages of Recursion
1. Recursive functions make the code look clean and elegant.
2. A complex task can be broken down into simpler sub-problems using recursion.
3. Sequence generation is easier with recursion than using some nested iteration.

Disadvantages of Recursion
1. Sometimes the logic behind recursion is hard to follow through.
2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
3. Recursive functions are hard to debug.

What are lambda functions in Python?


In Python, an anonymous function is a function that is defined without a name.
While normal functions are defined using the def keyword in Python, anonymous functions are defined
using the lambda keyword.
Hence, anonymous functions are also called lambda functions.

How to use lambda Functions in Python?


A lambda function in python has the following syntax.
Syntax of Lambda Function in python
lambda arguments: expression
Lambda functions can have any number of arguments but only one expression. The expression is evaluated
and returned. Lambda functions can be used wherever function objects are required.

Example of Lambda Function in python


Here is an example of lambda function that doubles the input value.
# Program to show the use of lambda functions
double = lambda x: x * 2

print(double(5))
Output
10
In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the
expression that gets evaluated and returned.
This function has no name. It returns a function object which is assigned to the identifier double. We can
now call it as a normal function. The statement
double = lambda x: x * 2
is nearly the same as:
def double(x):
return x * 2

Use of Lambda Function in python


We use lambda functions when we require a nameless function for a short period of time.
In Python, we generally use it as an argument to a higher-order function (a function that takes in other
functions as arguments). Lambda functions are used along with built-in functions like filter(), map() etc.
Example use with filter()
The filter() function in Python takes in a function and a list as arguments.
The function is called with all the items in the list and a new list is returned which contains items for which
the function evaluates to True.
Here is an example use of filter() function to filter out only even numbers from a list.
# Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(filter(lambda x: (x%2 == 0) , my_list))

print(new_list)
Output
[4, 6, 8, 12]
Example use with map()
The map() function in Python takes in a function and a list.
The function is called with all the items in the list and a new list is returned which contains items returned
by that function for each item.
Here is an example use of map() function to double all the items in a list.
# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)
Output
[2, 10, 8, 12, 16, 22, 6, 24]

Global Variables
In Python, a variable declared outside of the function or in global scope is known as a global variable. This
means that a global variable can be accessed inside or outside of the function.
Let's see an example of how a global variable is created in Python.
Example 1: Create a Global Variable
x = "global"

def foo():
print("x inside:", x)
foo()
print("x outside:", x)
Output
x inside: global
x outside: global
In the above code, we created x as a global variable and defined a foo() to print the global variable x.
Finally, we call the foo() which will print the value of x.
What if you want to change the value of x inside a function?
x = "global"
def foo():
x=x*2
print(x)
foo()
Output
UnboundLocalError: local variable 'x' referenced before assignment
The output shows an error because Python treats x as a local variable and x is also not defined inside foo().
To make this work, we use the global keyword. Visit Python Global Keyword to learn more.

Local Variables
A variable declared inside the function's body or in the local scope is known as a local variable.
Example 2: Accessing local variable outside the scope
def foo():
y = "local"
foo()
print(y)
Output
NameError: name 'y' is not defined
The output shows an error because we are trying to access a local variable y in a global scope whereas the
local variable only works inside foo() or local scope.

Let's see an example on how a local variable is created in Python.


Example 3: Create a Local Variable
Normally, we declare a variable inside the function to create a local variable.
def foo():
y = "local"
print(y)
foo()
Output
local
Let's take a look at the earlier problem where x was a global variable and we wanted to
modify x inside foo().

Global and local variables


Here, we will show how to use global variables and local variables in the same code.
Example 4: Using Global and Local variables in the same code
x = "global "

def foo():
global x
y = "local"
x=x*2
print(x)
print(y)
foo()
Output
global global
local
In the above code, we declare x as a global and y as a local variable in the foo(). Then, we use multiplication
operator * to modify the global variable x and we print both x and y.
After calling the foo(), the value of x becomes global global because we used the x * 2 to print two
times global. After that, we print the value of local variable y i.e local.

Example 5: Global variable and Local variable with same name


x=5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
Output
local x: 10
global x: 5
In the above code, we used the same name x for both global variable and local variable. We get a different
result when we print the same variable because the variable is declared in both scopes, i.e. the local scope
inside foo() and global scope outside foo().
When we print the variable inside foo() it outputs local x: 10. This is called the local scope of the variable.
Similarly, when we print the variable outside the foo(), it outputs global x: 5. This is called the global scope
of the variable.

Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the
variable can be neither in the local nor the global scope.
Let's see an example of how a nonlocal variable is used in Python.
We use nonlocal keywords to create nonlocal variables.
Example 6: Create a nonlocal variable
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
Output
inner: nonlocal
outer: nonlocal
In the above code, there is a nested inner() function. We use nonlocal keywords to create a nonlocal
variable. The inner() function is defined in the scope of another function outer().
Note : If we change the value of a nonlocal variable, the changes appear in the local variable.

What is the global keyword


In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create
a global variable and make changes to the variable in a local context.

Rules of global Keyword


The basic rules for global keyword in Python are:
 When we create a variable inside a function, it is local by default.
 When we define a variable outside of a function, it is global by default. You don't have to
use global keyword.
 We use global keyword to read and write a global variable inside a function.
 Use of global keyword outside a function has no effect.

Use of global Keyword


Let's take an example.
Example 1: Accessing global Variable From Inside a Function
c = 1 # global variable
def add():
print(c)
add()
When we run the above program, the output will be: 1
However, we may have some scenarios where we need to modify the global variable from inside a function.

Example 2: Modifying Global Variable From Inside the Function


c = 1 # global variable
def add():
c = c + 2 # increment c by 2
print(c)

add()
When we run the above program, the output shows an error:
UnboundLocalError: local variable 'c' referenced before assignment
This is because we can only access the global variable but cannot modify it from inside the function.
The solution for this is to use the global keyword.

Example 3: Changing Global Variable From Inside a Function using global


c = 0 # global variable

def add():
global c
c = c + 2 # increment by 2
print("Inside add():", c)

add()
print("In main:", c)
When we run the above program, the output will be:
Inside add(): 2
In main: 2
In the above program, we define c as a global keyword inside the add() function.
Then, we increment the variable c by 1, i.e c = c + 2. After that, we call the add() function. Finally, we print
the global variable c.
As we can see, change also occurred on the global variable outside the function, c = 2.

Global Variables Across Python Modules


In Python, we create a single module config.py to hold global variables and share information across Python
modules within the same program.
Here is how we can share global variables across the python modules.
Example 4: Share a global Variable Across Python Modules
Create a config.py file, to store global variables
a=0
b = "empty"
Create a update.py file, to change global variables
import config

config.a = 10
config.b = "alphabet"
Create a main.py file, to test changes in value
import config
import update

print(config.a)
print(config.b)
When we run the main.py file, the output will be
10
alphabet
In the above, we have created three files: config.py, update.py, and main.py.
The module config.py stores global variables of a and b. In the update.py file, we import
the config.py module and modify the values of a and b. Similarly, in the main.py file, we import
both config.py and update.py module. Finally, we print and test the values of global variables whether they
are changed or not.

Global in Nested Functions


Here is how you can use a global variable in nested function.
Example 5: Using a Global Variable in Nested Function
def foo():
x = 20
def bar():
global x
x = 25
print("Before calling bar: ", x)
print("Calling bar now")
bar()
print("After calling bar: ", x)
foo()
print("x in main: ", x)
The output is :
Before calling bar: 20
Calling bar now
After calling bar: 20
x in main: 25
In the above program, we declared a global variable inside the nested function bar().
Inside foo() function, x has no effect of the global keyword.
Before and after calling bar(), the variable x takes the value of local variable i.e x = 20. Outside of
the foo() function, the variable x will take value defined in the bar() function i.e x = 25. This is because we
have used global keyword in x to create global variable inside the bar() function (local scope).
If we make any changes inside the bar() function, the changes appear outside the local scope, i.e. foo().

What are modules in Python?


Modules refer to a file containing Python statements and definitions.
A file containing Python code, for example: example.py, is called a module, and its module name would
be example.
We use modules to break down large programs into small manageable and organized files. Furthermore,
modules provide reusability of code.
We can define our most used functions in a module and import it, instead of copying their definitions into
different programs.
Let us create a module. Type the following and save it as example.py.
# Python Module example

def add(a, b):


"""This program adds two
numbers and return the result"""
result = a + b
return result
Here, we have defined a function add() inside a module named example. The function takes in two numbers
and returns their sum.

How to import modules in Python?


We can import the definitions inside a module to another module or the interactive interpreter in Python.
We use the import keyword to do this. To import our previously defined module example, we type the
following in the Python prompt.
>>> import example
This does not import the names of the functions defined in example directly in the current symbol table. It
only imports the module name example there.
Using the module name we can access the function using the dot . operator. For example:
>>> example.add(4,5.5) 9.5
Python has tons of standard modules. You can check out the full list of Python standard modules and their
use cases. These files are in the Lib directory inside the location where you installed Python.
Standard modules can be imported the same way as we import our user-defined modules.
There are various ways to import modules. They are listed below..
Python import statement
We can import a module using the import statement and access the definitions inside it using the dot
operator as described above. Here is an example.
# import statement example
# to import standard module math

import math
print("The value of pi is", math.pi)
When you run the program, the output will be:
The value of pi is 3.141592653589793

Import with renaming


We can import a module by renaming it as follows:
# import module by renaming it

import math as m
print("The value of pi is", m.pi)
We have renamed the math module as m. This can save us typing time in some cases.
Note that the name math is not recognized in our scope. Hence, math.pi is invalid, and m.pi is the correct
implementation.

Python from...import statement


We can import specific names from a module without importing the module as a whole. Here is an example.
# import only pi from math module

from math import pi


print("The value of pi is", pi)
Here, we imported only the pi attribute from the math module.
In such cases, we don't use the dot operator. We can also import multiple attributes as follows:
>>> from math import pi, e
>>> pi
3.141592653589793
>>> e
2.718281828459045

Import all names


We can import all names(definitions) from a module using the following construct:
# import all names from the standard module math

from math import *


print("The value of pi is", pi)
Here, we have imported all the definitions from the math module. This includes all names visible in our
scope except those beginning with an underscore(private definitions).
Importing everything with the asterisk (*) symbol is not a good programming practice. This can lead to
duplicate definitions for an identifier. It also hampers the readability of our code.

Python Module Search Path


While importing a module, Python looks at several places. Interpreter first looks for a built-in module.
Then(if built-in module not found), Python looks into a list of directories defined in sys.path. The search is
in this order.
 The current directory.
 PYTHONPATH (an environment variable with a list of directories).
 The installation-dependent default directory.
>>> import sys
>>> sys.path
['',
'C:\\Python33\\Lib\\idlelib',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']
We can add and modify this list to add our own path.

Reloading a module
The Python interpreter imports a module only once during a session. This makes things more efficient. Here
is an example to show how this works.
Suppose we have the following code in a module named my_module.
# This module shows the effect of
# multiple imports and reload

print("This code got executed")


Now we see the effect of multiple imports.
>>> import my_module
This code got executed
>>> import my_module
>>> import my_module
We can see that our code got executed only once. This goes to say that our module was imported only once.
Now if our module changed during the course of the program, we would have to reload it.One way to do this
is to restart the interpreter. But this does not help much.
Python provides a more efficient way of doing this. We can use the reload() function inside the imp module
to reload a module. We can do it in the following ways:
>>> import imp
>>> import my_module
This code got executed
>>> import my_module
>>> imp.reload(my_module)
This code got executed
<module 'my_module' from '.\\my_module.py'>

The dir() built-in function


We can use the dir() function to find out names that are defined inside a module.
For example, we have defined a function add() in the module example that we had in the beginning.
We can use dir in example module in the following way:
>>> dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']
Here, we can see a sorted list of names (along with add). All other names that begin with an underscore are
default Python attributes associated with the module (not user-defined).
For example, the __name__ attribute contains the name of the module.
>>> import example
>>> example.__name__
'example'
All the names defined in our current namespace can be found out using the dir() function without any
arguments.
>>> a = 1
>>> b = "hello"
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']

What are packages?


We don't usually store all of our files on our computer in the same location. We use a well-organized
hierarchy of directories for easier access.
Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory.
Analogous to this, Python has packages for directories and modules for files.
As our application program grows larger in size with a lot of modules, we place similar modules in one
package and different modules in different packages. This makes a project (program) easy to manage and
conceptually clear.
Similarly, as a directory can contain subdirectories and files, a Python package can have sub-packages and
modules.
A directory must contain a file named __init__.py in order for Python to consider it as a package. This file
can be left empty but we generally place the initialization code for that package in this file.
Here is an example. Suppose we are developing a game. One possible organization of packages and modules
could be as shown in the figure below.

Importing module from a package


We can import modules from packages using the dot (.) operator.
For example, if we want to import the start module in the above example, it can be done as follows:
import Game.Level.start
Now, if this module contains a function named select_difficulty(), we must use the full name to reference it.
Game.Level.start.select_difficulty(2)
If this construct seems lengthy, we can import the module without the package prefix as follows:
from Game.Level import start
We can now call the function simply as follows:
start.select_difficulty(2)
Another way of importing just the required function (or class or variable) from a module within a package
would be as follows:
from Game.Level.start import select_difficulty
Now we can directly call this function.
select_difficulty(2)
Although easier, this method is not recommended. Using the full namespace avoids confusion and prevents
two same identifier names from colliding.
While importing packages, Python looks in the list of directories defined in sys.path, similar as for module
search path.
Number Data Type in Python
Python supports integers, floating-point numbers and complex numbers. They are defined as int, float,
and complex classes in Python.
Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an
integer whereas 5.0 is a floating-point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.
We can use the type() function to know which class a variable or a value belongs to and isinstance() function
to check if it belongs to a particular class.
Let's look at an example:
a=5
print(type(a))
print(type(5.0))
c = 5 + 3j
print(c + 3)
print(isinstance(c, complex))
When we run the above program, we get the following output:
<class 'int'>
<class 'float'>
(8+3j)
True
While integers can be of any length, a floating-point number is accurate only up to 15 decimal places (the
16th place is inaccurate).
The numbers we deal with every day are of the decimal (base 10) number system. But computer
programmers (generally embedded programmers) need to work with binary (base 2), hexadecimal (base 16)
and octal (base 8) number systems.
In Python, we can represent these numbers by appropriately placing a prefix before that number. The
following table lists these prefixes.
Number System Prefix

Binary '0b' or '0B'

Octal '0o' or '0O'

Hexadecimal '0x' or '0X'


Here are some examples
# Output: 107
print(0b1101011)

# Output: 253 (251 + 2)


print(0xFB + 0b10)

# Output: 13
print(0o15)
When you run the program, the output will be:
107 253 13

Type Conversion
We can convert one type of number into another. This is also known as coercion.
Operations like addition, subtraction coerce integer to float implicitly (automatically), if one of the operands
is float.
>>> 1 + 2.0 3.0
We can see above that 1 (integer) is coerced into 1.0 (float) for addition and the result is also a floating point
number.
We can also use built-in functions like int(), float() and complex() to convert between types explicitly. These
functions can even convert from strings.
>>> int(2.3) 2
>>> int(-2.8) -2
>>> float(5) 5.0
>>> complex('3+5j') (3+5j)
When converting from float to integer, the number gets truncated (decimal parts are removed).

Python Decimal
Python built-in class float performs some calculations that might amaze us. We all know that the sum of 1.1
and 2.2 is 3.3, but Python seems to disagree.
>>> (1.1 + 2.2) == 3.3
False
What is going on?
It turns out that floating-point numbers are implemented in computer hardware as binary fractions as the
computer only understands binary (0 and 1). Due to this reason, most of the decimal fractions we know,
cannot be accurately stored in our computer.
Let's take an example. We cannot represent the fraction 1/3 as a decimal number. This will give
0.33333333... which is infinitely long, and we can only approximate it.
It turns out that the decimal fraction 0.1 will result in an infinitely long binary fraction of
0.000110011001100110011... and our computer only stores a finite number of it.
This will only approximate 0.1 but never be equal. Hence, it is the limitation of our computer hardware and
not an error in Python.
>>> 1.1 + 2.2
3.3000000000000003
To overcome this issue, we can use the decimal module that comes with Python. While floating-point
numbers have precision up to 15 decimal places, the decimal module has user-settable precision.
Let's see the difference:
import decimal
print(0.1)
print(decimal.Decimal(0.1))
Output:
0.1 0.1000000000000000055511151231257827021181583404541015625
This module is used when we want to carry out decimal calculations as we learned in school.
It also preserves significance. We know 25.50 kg is more accurate than 25.5 kg as it has two significant
decimal places compared to one.
from decimal import Decimal as D
print(D('1.1') + D('2.2'))
print(D('1.2') * D('2.50'))
Output : 3.3 3.000
Notice the trailing zeroes in the above example.
We might ask, why not implement Decimal every time, instead of float? The main reason is efficiency.
Floating point operations are carried out must faster than Decimal operations.
When to use Decimal instead of float?
We generally use Decimal in the following cases.
 When we are making financial applications that need exact decimal representation.
 When we want to control the level of precision required.
 When we want to implement the notion of significant decimal places.

Python Fractions
Python provides operations involving fractional numbers through its fractions module.
A fraction has a numerator and a denominator, both of which are integers. This module has support for
rational number arithmetic.
We can create Fraction objects in various ways. Let's have a look at them.
import fractions
print(fractions.Fraction(1.5))
print(fractions.Fraction(5))
print(fractions.Fraction(1,3))
Output: 3/2 5 1/3
While creating Fraction from float, we might get some unusual results. This is due to the imperfect binary
floating point number representation as discussed in the previous section.
Fortunately, Fraction allows us to instantiate with string as well. This is the preferred option when using
decimal numbers.
import fractions

# As float
# Output: 2476979795053773/2251799813685248
print(fractions.Fraction(1.1))

# As string
# Output: 11/10
print(fractions.Fraction('1.1'))
Output
2476979795053773/2251799813685248
11/10
This data type supports all basic operations. Here are a few examples.
from fractions import Fraction as F
print(F(1, 3) + F(1, 3))
print(1 / F(5, 6))
print(F(-3, 10) > 0)
print(F(-3, 10) < 0)
Output: 2/3 6/5 False True

Python Mathematics
Python offers modules like math and random to carry out different mathematics like trigonometry,
logarithms, probability and statistics, etc.
import math
print(math.pi)
print(math.cos(math.pi))
print(math.exp(10))
print(math.log10(1000))
print(math.sinh(1))
print(math.factorial(6))
Output:
3.141592653589793
-1.0
22026.465794806718
3.0
1.1752011936438014
720
Here is the full list of functions and attributes available in the Python math module.
import random
print(random.randrange(10, 20))
x = ['a', 'b', 'c', 'd', 'e']
# Get random choice
print(random.choice(x))

# Shuffle x
random.shuffle(x)

# Print the shuffled x


print(x)

# Print random element


print(random.random())
When we run the above program we get the output as follows.(Values may be different due to the random
behavior)
18
e
['c', 'e', 'd', 'b', 'a']
0.5682821194654443
Here is the full list of functions and attributes available in the Python random module.

Python offers a range of compound data types often referred to as sequences. List is one of the most
frequently used and very versatile data types used in Python.

How to create a list?


In Python programming, a list is created by placing all the items (elements) inside square brackets [],
separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.).
# empty list
my_list = []

# list of integers
my_list = [1, 2, 3]

# list with mixed data types


my_list = [1, "Hello", 3.4]
A list can also have another list as an item. This is called a nested list.
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]

How to access elements from a list?


There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5
elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an IndexError. The index must be an integer. We can't
use float or other types, this will result in TypeError.
Nested lists are accessed using nested indexing.
# List indexing

my_list = ['p', 'r', 'o', 'b', 'e']

# Output: p
print(my_list[0])

# Output: o
print(my_list[2])

# Output: e
print(my_list[4])
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]

# Nested indexing
print(n_list[0][1])

print(n_list[1][3])

# Error! Only integer can be used for indexing


print(my_list[4.0])
Output: p o e a 5
Traceback (most recent call last):
File "<string>", line 21, in <module>
TypeError: list indices must be integers or slices, not float

Negative indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second
last item and so on.
# Negative indexing in lists
my_list = ['p','r','o','b','e']

print(my_list[-1])
print(my_list[-5])
When we run the above program, we will get the following output:
e
p

How to slice lists in Python?


We can access a range of items in a list by using the slicing operator :(colon).
# List slicing in Python

my_list = ['p','r','o','g','r','a','m','i','z']

# elements 3rd to 5th


print(my_list[2:5])

# elements beginning to 4th


print(my_list[:-5])

# elements 6th to end


print(my_list[5:])

# elements beginning to end


print(my_list[:])
Output: ['o', 'g', 'r'] ['p', 'r', 'o', 'g'] ['a', 'm', 'i', 'z'] ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']
Slicing can be best visualized by considering the index to be between the elements as shown below. So if we
want to access a range, we need two indices that will slice that portion from the list.

How to change or add elements to a list?


Lists are mutable, meaning their elements can be changed unlike string or tuple.
We can use the assignment operator (=) to change an item or a range of items.
# Correcting mistake values in a list
odd = [2, 4, 6, 8]

# change the 1st item


odd[0] = 1

print(odd)

# change 2nd to 4th items


odd[1:4] = [3, 5, 7]

print(odd)
Output
[1, 4, 6, 8]
[1, 3, 5, 7]
We can add one item to a list using the append() method or add several items using extend() method.
# Appending and Extending lists in Python
odd = [1, 3, 5]

odd.append(7)

print(odd)

odd.extend([9, 11, 13])

print(odd)
Output
[1, 3, 5, 7]
[1, 3, 5, 7, 9, 11, 13]
We can also use + operator to combine two lists. This is also called concatenation.
The * operator repeats a list for the given number of times.
# Concatenating and repeating lists
odd = [1, 3, 5]

print(odd + [9, 7, 5])

print(["re"] * 3)
Output
[1, 3, 5, 9, 7, 5]
['re', 're', 're']
Furthermore, we can insert one item at a desired location by using the method insert() or insert multiple
items by squeezing it into an empty slice of a list.
# Demonstration of list insert() method
odd = [1, 9]
odd.insert(1,3)

print(odd)

odd[2:2] = [5, 7]

print(odd)
Output
[1, 3, 9]
[1, 3, 5, 7, 9]

How to delete or remove elements from a list?


We can delete one or more items from a list using the keyword del. It can even delete the list entirely.
# Deleting list items
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']

# delete one item


del my_list[2]

print(my_list)

# delete multiple items


del my_list[1:5]

print(my_list)

# delete entire list


del my_list

# Error: List not defined


print(my_list)
Output
['p', 'r', 'b', 'l', 'e', 'm']
['p', 'm']
Traceback (most recent call last):
File "<string>", line 18, in <module>
NameError: name 'my_list' is not defined
We can use remove() method to remove the given item or pop() method to remove an item at the given
index.
The pop() method removes and returns the last item if the index is not provided. This helps us implement
lists as stacks (first in, last out data structure).
We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')

# Output: ['r', 'o', 'b', 'l', 'e', 'm']


print(my_list)

# Output: 'o'
print(my_list.pop(1))

# Output: ['r', 'b', 'l', 'e', 'm']


print(my_list)
# Output: 'm'
print(my_list.pop())

# Output: ['r', 'b', 'l', 'e']


print(my_list)

my_list.clear()

# Output: []
print(my_list)
Output
['r', 'o', 'b', 'l', 'e', 'm']
o
['r', 'b', 'l', 'e', 'm']
m
['r', 'b', 'l', 'e']
[]
Finally, we can also delete items in a list by assigning an empty list to a slice of elements.
>>> my_list = ['p','r','o','b','l','e','m']
>>> my_list[2:3] = []
>>> my_list
['p', 'r', 'b', 'l', 'e', 'm']
>>> my_list[2:5] = []
>>> my_list
['p', 'r', 'm']

Python List Methods


Methods that are available with list objects in Python programming are tabulated below.
They are accessed as list.method(). Some of the methods have already been used above.
Python List Methods

append() - Add an element to the end of the list

extend() - Add all elements of a list to the another list

insert() - Insert an item at the defined index

remove() - Removes an item from the list

pop() - Removes and returns an element at the given index

clear() - Removes all items from the list

index() - Returns the index of the first matched item

count() - Returns the count of the number of items passed as an argument

sort() - Sort items in a list in ascending order

reverse() - Reverse the order of items in the list


copy() - Returns a shallow copy of the list
Some examples of Python list methods:
# Python list methods
my_list = [3, 8, 1, 6, 0, 8, 4]

# Output: 1
print(my_list.index(8))

# Output: 2
print(my_list.count(8))

my_list.sort()

# Output: [0, 1, 3, 4, 6, 8, 8]
print(my_list)

my_list.reverse()

# Output: [8, 8, 6, 4, 3, 1, 0]
print(my_list)
Output
1
2
[0, 1, 3, 4, 6, 8, 8]
[8, 8, 6, 4, 3, 1, 0]

List Comprehension: Elegant way to create new List


List comprehension is an elegant and concise way to create a new list from an existing list in Python.
A list comprehension consists of an expression followed by for statement inside square brackets.
Here is an example to make a list with each item being increasing power of 2.
pow2 = [2 ** x for x in range(10)]
print(pow2)
Output
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
This code is equivalent to:
pow2 = []
for x in range(10):
pow2.append(2 ** x)
A list comprehension can optionally contain more for or if statements. An optional if statement can filter out
items for the new list. Here are some examples.
>>> pow2 = [2 ** x for x in range(10) if x > 5]
>>> pow2
[64, 128, 256, 512]
>>> odd = [x for x in range(20) if x % 2 == 1]
>>> odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> [x+y for x in ['Python ','C '] for y in ['Language','Programming']]
['Python Language', 'Python Programming', 'C Language', 'C Programming']

Other List Operations in Python


List Membership Test
We can test if an item exists in a list or not, using the keyword in.
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
# Output: True
print('p' in my_list)

# Output: False
print('a' in my_list)

# Output: True
print('c' not in my_list)
Output: True False True

Iterating Through a List


Using a for loop we can iterate through each item in a list.
for fruit in ['apple','banana','mango']:
print("I like",fruit)
Output: I like apple I like banana I like mango

A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements
of a tuple once it is assigned whereas we can change the elements of a list.

Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas. The
parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).
# Different types of tuples

# Empty tuple
my_tuple = ()
print(my_tuple)

# Tuple having integers


my_tuple = (1, 2, 3)
print(my_tuple)

# tuple with mixed datatypes


my_tuple = (1, "Hello", 3.4)
print(my_tuple)

# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
Output: () (1, 2, 3) (1, 'Hello', 3.4) ('mouse', [8, 4, 6], (1, 2, 3))
A tuple can also be created without using parentheses. This is known as tuple packing.
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible


a, b, c = my_tuple

print(a) #3
print(b) # 4.6
print(c) # dog
Output:(3, 4.6, 'dog') 3 4.6 dog
Creating a tuple with one element is a bit tricky.
Having one element within parentheses is not enough. We will need a trailing comma to indicate that it is, in
fact, a tuple.
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element


my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>
Output: <class 'str'> <class 'tuple'> <class 'tuple'>

Access Tuple Elements


There are various ways in which we can access the elements of a tuple.
1. Indexing
We can use the index operator [] to access an item in a tuple, where the index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5. Trying to access an index outside of the tuple
index range(6,7,... in this example) will raise an IndexError.
The index must be an integer, so we cannot use float or other types. This will result in TypeError.
Likewise, nested tuples are accessed using nested indexing, as shown in the example below.
# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'

# IndexError: list index out of range


# print(my_tuple[6])

# Index must be an integer


# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3]) # 's'
print(n_tuple[1][1]) #4
Output: p t s 4

2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
Output: t p
3. Slicing
We can access a range of items in a tuple by using the slicing operator colon :.
# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th


# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd


# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end


# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end


# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
Output: ('r', 'o', 'g') ('p', 'r') ('i', 'z') ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
Slicing can be best visualized by considering the index to be between the elements as shown below. So if we
want to access a range, we need the index that will slice the portion from the tuple.

Changing a Tuple
Unlike lists, tuples are immutable.
This means that elements of a tuple cannot be changed once they have been assigned. But, if the element is
itself a mutable data type like list, its nested items can be changed.
We can also assign a tuple to different values (reassignment).
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])

# TypeError: 'tuple' object does not support item assignment


# my_tuple[1] = 9

# However, item of mutable element can be changed


my_tuple[3][0] = 9 # Output: (4, 2, 3, [9, 5])
print(my_tuple)

# Tuples can be reassigned


my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
Output: (4, 2, 3, [9, 5]) ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
We can use + operator to combine two tuples. This is called concatenation.
We can also repeat the elements in a tuple for a given number of times using the * operator.
Both + and * operations result in a new tuple.
# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))

# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",) * 3)
Output: (1, 2, 3, 4, 5, 6) ('Repeat', 'Repeat', 'Repeat')

Deleting a Tuple
As discussed above, we cannot change the elements in a tuple. It means that we cannot delete or remove
items from a tuple.
Deleting a tuple entirely, however, is possible using the keyword del.
# Deleting tuples
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# can't delete items


# TypeError: 'tuple' object doesn't support item deletion
# del my_tuple[3]

# Can delete an entire tuple


del my_tuple

# NameError: name 'my_tuple' is not defined


print(my_tuple)
Output
Traceback (most recent call last):
File "<string>", line 12, in <module>
NameError: name 'my_tuple' is not defined

Tuple Methods
Methods that add items or remove items are not available with tuple. Only the following two methods are
available.
Some examples of Python tuple methods:
my_tuple = ('a', 'p', 'p', 'l', 'e',)

print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
Output : 2 3

Other Tuple Operations


1. Tuple Membership Test
We can test if an item exists in a tuple or not, using the keyword in.
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)

# In operation
print('a' in my_tuple)
print('b' in my_tuple)

# Not in operation
print('g' not in my_tuple)
Output: True False True
2. Iterating Through a Tuple
We can use a for loop to iterate through each item in a tuple.
# Using a for loop to iterate through a tuple
for name in ('John', 'Kate'):
print("Hello", name)
Output: Hello John Hello Kate

Advantages of Tuple over List


Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain
advantages of implementing a tuple over a list. Below listed are some of the main advantages:
 We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar)
data types.
 Since tuples are immutable, iterating through a tuple is faster than with list. So there is a slight
performance boost.
 Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not
possible.
 If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-
protected.

What is String in Python?


A string is a sequence of characters.
A character is simply a symbol. For example, the English language has 26 characters.
Computers do not deal with characters, they deal with numbers (binary). Even though you may see
characters on your screen, internally it is stored and manipulated as a combination of 0s and 1s.
This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and
Unicode are some of the popular encodings used.
In Python, a string is a sequence of Unicode characters. Unicode was introduced to include every character
in all languages and bring uniformity in encoding. You can learn about Unicode from Python Unicode.

How to create a string in Python?


Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can
be used in Python but generally used to represent multiline strings and docstrings.
# defining strings in Python
# all of the following are equivalent
my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

my_string = '''Hello'''
print(my_string)

# triple quotes string can extend multiple lines


my_string = """Hello, welcome to
the world of Python"""
print(my_string)
When you run the program, the output will be:
Hello
Hello
Hello
Hello, welcome to
the world of Python
How to access characters in a string?
We can access individual characters using indexing and a range of characters using slicing. Index starts from
0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We
can't use floats or other types, this will result into TypeError.
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in
a string by using the slicing operator :(colon).
#Accessing string characters in Python
str = 'programiz'
print('str = ', str)

#first character
print('str[0] = ', str[0])

#last character
print('str[-1] = ', str[-1])

#slicing 2nd to 5th character


print('str[1:5] = ', str[1:5])

#slicing 6th to 2nd last character


print('str[5:-2] = ', str[5:-2])
When we run the above program, we get the following output:
str = programiz
str[0] = p
str[-1] = z
str[1:5] = rogr
str[5:-2] = am
If we try to access an index out of the range or use numbers other than an integer, we will get errors.
# index must be in range
>>> my_string[15]
...
IndexError: string index out of range

# index must be an integer


>>> my_string[1.5]
...
TypeError: string indices must be integers
Slicing can be best visualized by considering the index to be between the elements as shown below.
If we want to access a range, we need the index that will slice the portion from the string.

How to change or delete a string?


Strings are immutable. This means that elements of a string cannot be changed once they have been
assigned. We can simply reassign different strings to the same name.
>>> my_string = 'programiz'
>>> my_string[5] = 'a'
...
TypeError: 'str' object does not support item assignment
>>> my_string = 'Python'
>>> my_string
'Python'
We cannot delete or remove characters from a string. But deleting the string entirely is possible using
the del keyword.
>>> del my_string[1]
...
TypeError: 'str' object doesn't support item deletion
>>> del my_string
>>> my_string
...
NameError: name 'my_string' is not defined

Python String Operations


There are many operations that can be performed with strings which makes it one of the most used data
types in Python.
To learn more about the data types available in Python visit: Python Data Types
Concatenation of Two or More Strings
Joining of two or more strings into a single one is called concatenation.
The + operator does this in Python. Simply writing two string literals together also concatenates them.
The * operator can be used to repeat the string for a given number of times.
# Python String Operations
str1 = 'Hello'
str2 ='World!'

# using +
print('str1 + str2 = ', str1 + str2)

# using *
print('str1 * 3 =', str1 * 3)
When we run the above program, we get the following output:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
Writing two string literals together also concatenates them like + operator.
If we want to concatenate strings in different lines, we can use parentheses.
>>> # two string literals together
>>> 'Hello ''World!'
'Hello World!'

>>> # using parentheses


>>> s = ('Hello '
... 'World')
>>> s
'Hello World'

Iterating Through a string


We can iterate through a string using a for loop. Here is an example to count the number of 'l's in a string.
# Iterating through a string
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
When we run the above program, we get the following output:
3 letters found
String Membership Test
We can test if a substring exists within a string or not, using the keyword in.
>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False

Built-in functions to Work with Python


Various built-in functions that work with sequence work with strings as well.
Some of the commonly used ones are enumerate() and len(). The enumerate() function returns an enumerate
object. It contains the index and value of all the items in the string as pairs. This can be useful for iteration.
Similarly, len() returns the length (number of characters) of the string.
str = 'cold'

# enumerate()
list_enumerate = list(enumerate(str))
print('list(enumerate(str) = ', list_enumerate)

#character count
print('len(str) = ', len(str))
When we run the above program, we get the following output:
list(enumerate(str) = [(0, 'c'), (1, 'o'), (2, 'l'), (3, 'd')]
len(str) = 4

Python String Formatting


Escape Sequence
If we want to print a text like He said, "What's there?", we can neither use single quotes nor double quotes.
This will result in a SyntaxError as the text itself contains both single and double quotes.
>>> print("He said, "What's there?"")
...
SyntaxError: invalid syntax
>>> print('He said, "What's there?"')
...
SyntaxError: invalid syntax
One way to get around this problem is to use triple quotes. Alternatively, we can use escape sequences.
An escape sequence starts with a backslash and is interpreted differently. If we use a single quote to
represent a string, all the single quotes inside the string must be escaped. Similar is the case with double
quotes. Here is how it can be done to represent the above text.
# using triple quotes
print('''He said, "What's there?"''')

# escaping single quotes


print('He said, "What\'s there?"')

# escaping double quotes


print("He said, \"What's there?\"")
When we run the above program, we get the following output:
He said, "What's there?"
He said, "What's there?"
He said, "What's there?"
Here is a list of all the escape sequences supported by Python.
Escape Sequence Description

\newline Backslash and newline ignored


\\ Backslash

\' Single quote

\" Double quote

\a ASCII Bell

\b ASCII Backspace

\f ASCII Formfeed

\n ASCII Linefeed

\r ASCII Carriage Return

\t ASCII Horizontal Tab

\v ASCII Vertical Tab

\ooo Character with octal value ooo

\xHH Character with hexadecimal value HH


Here are some examples
>>> print("C:\\Python32\\Lib")
C:\Python32\Lib

>>> print("This is printed\nin two lines")


This is printed
in two lines

>>> print("This is \x48\x45\x58 representation")


This is HEX representation

Raw String to ignore escape sequence


Sometimes we may wish to ignore the escape sequences inside a string. To do this we can place r or R in
front of the string. This will imply that it is a raw string and any escape sequence inside it will be ignored.
>>> print("This is \x61 \ngood example")
This is a
good example
>>> print(r"This is \x61 \ngood example")
This is \x61 \ngood example

The format() Method for Formatting Strings


The format() method that is available with the string object is very versatile and powerful in formatting
strings. Format strings contain curly braces {} as placeholders or replacement fields which get replaced.
We can use positional arguments or keyword arguments to specify the order.
# Python string format() method

# default(implicit) order
default_order = "{}, {} and {}".format('John','Bill','Sean')
print('\n--- Default Order ---')
print(default_order)

# order using positional argument


positional_order = "{1}, {0} and {2}".format('John','Bill','Sean')
print('\n--- Positional Order ---')
print(positional_order)

# order using keyword argument


keyword_order = "{s}, {b} and {j}".format(j='John',b='Bill',s='Sean')
print('\n--- Keyword Order ---')
print(keyword_order)
When we run the above program, we get the following output:
--- Default Order ---
John, Bill and Sean

--- Positional Order ---


Bill, John and Sean

--- Keyword Order ---


Sean, Bill and John
The format() method can have optional format specifications. They are separated from the field name using
colon. For example, we can left-justify <, right-justify > or center ^ a string in the given space.
We can also format integers as binary, hexadecimal, etc. and floats can be rounded or displayed in the
exponent format. There are tons of formatting you can use. Visit here for all the string formatting available
with the format() method.
>>> # formatting integers
>>> "Binary representation of {0} is {0:b}".format(12)
'Binary representation of 12 is 1100'

>>> # formatting floats


>>> "Exponent representation: {0:e}".format(1566.345)
'Exponent representation: 1.566345e+03'

>>> # round off


>>> "One third is: {0:.3f}".format(1/3)
'One third is: 0.333'

>>> # string alignment


>>> "|{:<10}|{:^10}|{:>10}|".format('butter','bread','ham')
'|butter | bread | ham|'

Old style formatting


We can even format strings like the old sprintf() style used in C programming language. We use
the % operator to accomplish this.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457

Common Python String Methods


There are numerous methods available with the string object. The format() method that we mentioned above
is one of them. Some of the commonly used methods are lower(), upper(), join(), split(), find(), replace() etc.
Here is a complete list of all the built-in methods to work with strings in Python.
>>> "PrOgRaMiZ".lower()
'programiz'
>>> "PrOgRaMiZ".upper()
'PROGRAMIZ'
>>> "This will split all words into a list".split()
['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
>>> ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
'This will join all words into a string'
>>> 'Happy New Year'.find('ew')
7
>>> 'Happy New Year'.replace('Happy','Brilliant')
'Brilliant New Year'

Set
A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable
(cannot be changed).
However, a set itself is mutable. We can add or remove items from it.
Sets can also be used to perform mathematical set operations like union, intersection, symmetric difference,
etc.

Creating Python Sets


A set is created by placing all the items (elements) inside curly braces {}, separated by comma, or by using
the built-in set() function.
It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a
set cannot have mutable elements like lists, sets or dictionaries as its elements.
# Different types of sets in Python
# set of integers
my_set = {1, 2, 3}
print(my_set)

# set of mixed datatypes


my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set)
Output: {1, 2, 3} {1.0, (1, 2, 3), 'Hello'}
Try the following examples as well.
# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list


# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

# set cannot have mutable items


# here [3, 4] is a mutable list
# this will cause an error.

my_set = {1, 2, [3, 4]}


Output: {1, 2, 3, 4} {1, 2, 3}
Traceback (most recent call last):
File "<string>", line 15, in <module>
my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'
Creating an empty set is a bit tricky.
Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements, we
use the set() function without any argument.
# Distinguish set and dictionary while creating empty set

# initialize a with {}
a = {}

# check data type of a


print(type(a))

# initialize a with set()


a = set()

# check data type of a


print(type(a))
Output: <class 'dict'> <class 'set'>

Modifying a set in Python


Sets are mutable. However, since they are unordered, indexing has no meaning.
We cannot access or change an element of a set using indexing or slicing. Set data type does not support it.
We can add a single element using the add() method, and multiple elements using the update() method.
The update() method can take tuples, lists, strings or other sets as its argument. In all cases, duplicates are
avoided.
# initialize my_set
my_set = {1, 3}
print(my_set)

# if you uncomment line 9,


# you will get an error
# TypeError: 'set' object does not support indexing

# my_set[0]

# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)

# add multiple elements


# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)

# add list and set


# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
Output: {1, 3} {1, 2, 3} {1, 2, 3, 4} {1, 2, 3, 4, 5, 6, 8}

Removing elements from a set


A particular item can be removed from a set using the methods discard() and remove().
The only difference between the two is that the discard() function leaves a set unchanged if the element is
not present in the set. On the other hand, the remove() function will raise an error in such a condition (if
element is not present in the set).
The following example will illustrate this.
# Difference between discard() and remove()

# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)

# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)

# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)

# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)

# remove an element
# not present in my_set
# you will get an error.
# Output: KeyError

my_set.remove(2)
Output: {1, 3, 4, 5, 6} {1, 3, 5, 6} {1, 3, 5} {1, 3, 5}
Traceback (most recent call last):
File "<string>", line 28, in <module>
KeyError: 2
Similarly, we can remove and return an item using the pop() method.
Since set is an unordered data type, there is no way of determining which item will be popped. It is
completely arbitrary.
We can also remove all the items from a set using the clear() method.
# initialize my_set
# Output: set of unique elements
my_set = set("HelloWorld")
print(my_set)

# pop an element
# Output: random element
print(my_set.pop())

# pop another element


my_set.pop()
print(my_set)

# clear my_set
# Output: set()
my_set.clear()
print(my_set)

print(my_set)
Output: {'H', 'l', 'r', 'W', 'o', 'd', 'e'} H {'r', 'W', 'o', 'd', 'e'} set()

Python Set Operations


Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric
difference. We can do this with operators or methods.
Let us consider the following two sets for the following operations.
>>> A = {1, 2, 3, 4, 5}
>>> B = {4, 5, 6, 7, 8}
Set Union

Union of A and B is a set of all elements from both sets.


Union is performed using | operator. Same can be accomplished using the union() method.
# Set union method
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

# use | operator
# Output: {1, 2, 3, 4, 5, 6, 7, 8}
print(A | B)
Output: {1, 2, 3, 4, 5, 6, 7, 8}
Try the following examples on Python shell.
# use union function
>>> A.union(B)
{1, 2, 3, 4, 5, 6, 7, 8}

# use union function on B


>>> B.union(A)
{1, 2, 3, 4, 5, 6, 7, 8}

Set Intersection

Intersection of A and B is a set of elements that are common in both the sets.
Intersection is performed using & operator. Same can be accomplished using the intersection() method.
# Intersection of sets
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use & operator
# Output: {4, 5}
print(A & B)
Output: {4, 5}
Try the following examples on Python shell.
# use intersection function on A
>>> A.intersection(B)
{4, 5}

# use intersection function on B


>>> B.intersection(A)
{4, 5}

Set Difference

Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B.
Similarly, B - A is a set of elements in B but not in A.
Difference is performed using - operator. Same can be accomplished using the difference() method.
# Difference of two sets
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

# use - operator on A
# Output: {1, 2, 3}
print(A - B)
Output: {1, 2, 3}
Try the following examples on Python shell.
# use difference function on A
>>> A.difference(B)
{1, 2, 3}

# use - operator on B
>>> B - A
{8, 6, 7}

# use difference function on B


>>> B.difference(A)
{8, 6, 7}

Set Symmetric Difference

Symmetric Difference of A and B is a set of elements in A and B but not in both (excluding the
intersection).
Symmetric difference is performed using ^ operator. Same can be accomplished using the
method symmetric_difference().
# Symmetric difference of two sets
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

# use ^ operator
# Output: {1, 2, 3, 6, 7, 8}
print(A ^ B)
Output: {1, 2, 3, 6, 7, 8}
Try the following examples on Python shell.
# use symmetric_difference function on A
>>> A.symmetric_difference(B)
{1, 2, 3, 6, 7, 8}

# use symmetric_difference function on B


>>> B.symmetric_difference(A)
{1, 2, 3, 6, 7, 8}

Other Python Set Methods


There are many set methods, some of which we have already used above. Here is a list of all the methods
that are available with the set objects:
Method Description

add() Adds an element to the set

clear() Removes all elements from the set

copy() Returns a copy of the set

difference() Returns the difference of two or more sets as a new set

difference_update() Removes all elements of another set from this set

Removes an element from the set if it is a member. (Do nothing if the


discard()
element is not in set)

intersection() Returns the intersection of two sets as a new set

intersection_update() Updates the set with the intersection of itself and another

isdisjoint() Returns True if two sets have a null intersection

issubset() Returns True if another set contains this set

issuperset() Returns True if this set contains another set

Removes and returns an arbitrary set element. Raises KeyError if the


pop()
set is empty
Removes an element from the set. If the element is not a member,
remove()
raises a KeyError

symmetric_difference() Returns the symmetric difference of two sets as a new set

symmetric_difference_up
Updates a set with the symmetric difference of itself and another
date()

union() Returns the union of sets in a new set

update() Updates the set with the union of itself and others

Other Set Operations


Set Membership Test
We can test if an item exists in a set or not, using the in keyword.
# in keyword in a set
# initialize my_set
my_set = set("apple")

# check if 'a' is present


# Output: True
print('a' in my_set)

# check if 'p' is present


# Output: False
print('p' not in my_set)
Output : True False

Iterating Through a Set


We can iterate through each item in a set using a for loop.
>>> for letter in set("apple"):
... print(letter)
...
a
p
e
l

Built-in Functions with Set


Built-in functions like all(), any(), enumerate(), len(), max(), min(), sorted(), sum() etc. are commonly used
with sets to perform different tasks.
Function Description

all() Returns True if all elements of the set are true (or if the set is empty).

any() Returns True if any element of the set is true. If the set is empty, returns False.

Returns an enumerate object. It contains the index and value for all the items of the
enumerate()
set as a pair.
len() Returns the length (the number of items) in the set.

max() Returns the largest item in the set.

min() Returns the smallest item in the set.

sorted() Returns a new sorted list from elements in the set(does not sort the set itself).

sum() Returns the sum of all elements in the set.

Python Frozenset
Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once
assigned. While tuples are immutable lists, frozensets are immutable sets.
Sets being mutable are unhashable, so they can't be used as dictionary keys. On the other hand, frozensets
are hashable and can be used as keys to a dictionary.
Frozensets can be created using the frozenset() function.
This data type supports methods
like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and unio
n(). Being immutable, it does not have methods that add or remove elements.
# Frozensets
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
Try these examples on Python shell.
>>> A.isdisjoint(B)
False
>>> A.difference(B)
frozenset({1, 2})
>>> A | B
frozenset({1, 2, 3, 4, 5, 6})
>>> A.add(3)
...
AttributeError: 'frozenset' object has no attribute 'add'

Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair.
Dictionaries are optimized to retrieve values when the key is known.

Creating Python Dictionary


Creating a dictionary is as simple as placing items inside curly braces {} separated by commas.
An item has a key and a corresponding value that is expressed as a pair (key: value).
While the values can be of any data type and can repeat, keys must be of immutable type
(string, number or tuple with immutable elements) and must be unique.
# empty dictionary
my_dict = {}

# dictionary with integer keys


my_dict = {1: 'apple', 2: 'ball'}

# dictionary with mixed keys


my_dict = {'name': 'John', 1: [2, 4, 3]}

# using dict()
my_dict = dict({1:'apple', 2:'ball'})
# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])
As you can see from above, we can also create a dictionary using the built-in dict() function.

Accessing Elements from Dictionary


While indexing is used with other data types to access values, a dictionary uses keys. Keys can be used
either inside square brackets [] or with the get() method.
If we use the square brackets [], KeyError is raised in case a key is not found in the dictionary. On the other
hand, the get() method returns None if the key is not found.
# get vs [] for retrieving elements
my_dict = {'name': 'Jack', 'age': 26}

# Output: Jack
print(my_dict['name'])

# Output: 26
print(my_dict.get('age'))

# Trying to access keys which doesn't exist throws error


# Output None
print(my_dict.get('address'))

# KeyError
print(my_dict['address'])
Output: Jack 26 None
Traceback (most recent call last):
File "<string>", line 15, in <module>
print(my_dict['address'])
KeyError: 'address'

Changing and Adding Dictionary elements


Dictionaries are mutable. We can add new items or change the value of existing items using an assignment
operator.
If the key is already present, then the existing value gets updated. In case the key is not present, a new (key:
value) pair is added to the dictionary.
# Changing and adding Dictionary Elements
my_dict = {'name': 'Jack', 'age': 26}

# update value
my_dict['age'] = 27

#Output: {'age': 27, 'name': 'Jack'}


print(my_dict)

# add item
my_dict['address'] = 'Downtown'

# Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}


print(my_dict)
Output
{'name': 'Jack', 'age': 27}
{'name': 'Jack', 'age': 27, 'address': 'Downtown'}
Removing elements from Dictionary
We can remove a particular item in a dictionary by using the pop() method. This method removes an item
with the provided key and returns the value.
The popitem() method can be used to remove and return an arbitrary (key, value) item pair from the
dictionary. All the items can be removed at once, using the clear() method.
We can also use the del keyword to remove individual items or the entire dictionary itself.
# Removing elements from a dictionary

# create a dictionary
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

# remove a particular item, returns its value


print(squares.pop(4)) # Output: 16

print(squares) # Output: {1: 1, 2: 4, 3: 9, 5: 25}

# remove an arbitrary item, return (key,value)


print(squares.popitem()) # Output: (5, 25)

print(squares) # Output: {1: 1, 2: 4, 3: 9}

squares.clear() # remove all items

print(squares) # Output: {}

del squares # delete the dictionary itself

print(squares) # Throws Error


Output: 16 {1: 1, 2: 4, 3: 9, 5: 25} (5, 25) {1: 1, 2: 4, 3: 9} {}
Traceback (most recent call last):
File "<string>", line 30, in <module>
print(squares)
NameError: name 'squares' is not defined

Python Dictionary Methods


Methods that are available with a dictionary are tabulated below. Some of them have already been used in
the above examples.
Method Description

clear() Removes all items from the dictionary.

copy() Returns a shallow copy of the dictionary.

fromkeys(seq[,
Returns a new dictionary with keys from seq and value equal to v (defaults to None).
v])

get(key[,d]) Returns the value of the key. If the key does not exist, returns d (defaults to None).

items() Return a new object of the dictionary's items in (key, value) format.

keys() Returns a new object of the dictionary's keys.


Removes the item with the key and returns its value or d if key is not found. If d is
pop(key[,d])
not provided and the key is not found, it raises KeyError.

Removes and returns an arbitrary item (key, value). Raises KeyError if the
popitem()
dictionary is empty.

setdefault(key[ Returns the corresponding value if the key is in the dictionary. If not, inserts the key
,d]) with a value of d and returns d (defaults to None).

Updates the dictionary with the key/value pairs from other, overwriting existing
update([other])
keys.

values() Returns a new object of the dictionary's values


Here are a few example use cases of these methods.
# Dictionary Methods
marks = {}.fromkeys(['Math', 'English', 'Science'], 0)

print(marks) # Output: {'English': 0, 'Math': 0, 'Science': 0}

for item in marks.items():


print(item)

print(list(sorted(marks.keys()))) # Output: ['English', 'Math', 'Science']


Output
{'Math': 0, 'English': 0, 'Science': 0}
('Math', 0)
('English', 0)
('Science', 0)
['English', 'Math', 'Science']

Python Dictionary Comprehension


Dictionary comprehension is an elegant and concise way to create a new dictionary from an iterable in
Python.
Dictionary comprehension consists of an expression pair (key: value) followed by a for statement inside
curly braces {}.
Here is an example to make a dictionary with each item being a pair of a number and its square.
# Dictionary Comprehension
squares = {x: x*x for x in range(6)}

print(squares)
Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
This code is equivalent to
squares = {}
for x in range(6):
squares[x] = x*x
print(squares)
Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
A dictionary comprehension can optionally contain more for or if statements.
An optional if statement can filter out items to form the new dictionary.
Here are some examples to make a dictionary with only odd items.
# Dictionary Comprehension with if conditional
odd_squares = {x: x*x for x in range(11) if x % 2 == 1}
print(odd_squares)
Output: {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
To learn more dictionary comprehensions, visit Python Dictionary Comprehension.

Other Dictionary Operations


Dictionary Membership Test
We can test if a key is in a dictionary or not using the keyword in. Notice that the membership test is only
for the keys and not for the values.
# Membership Test for Dictionary Keys
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}

print(1 in squares) # Output: True

print(2 not in squares) # Output: True

# membership tests for key only not value


print(49 in squares) # Output: False
Output: True True False
Iterating Through a Dictionary
We can iterate through each key in a dictionary using a for loop.
# Iterating through a Dictionary
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])
Output: 1 9 25 49 81

Dictionary Built-in Functions


Built-in functions like all(), any(), len(), cmp(), sorted(), etc. are commonly used with dictionaries to
perform different tasks.
Function Description

all() Return True if all keys of the dictionary are True (or if the dictionary is empty).

any() Return True if any key of the dictionary is true. If the dictionary is empty, return False.

len() Return the length (the number of items) in the dictionary.

cmp() Compares items of two dictionaries. (Not available in Python 3)

sorted() Return a new sorted list of keys in the dictionary.


Here are some examples that use built-in functions to work with a dictionary.
# Dictionary Built-in Functions
squares = {0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(all(squares)) # Output: False
print(any(squares)) # Output: True
print(len(squares)) # Output: 6

print(sorted(squares)) # Output: [0, 1, 3, 5, 7, 9]


Output: False True 6 [0, 1, 3, 5, 7, 9]

You might also like