Unit 1 Python Basics

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 82

Lexicon MILE

Python for Data Science

Unit 1 Python Basics


Course Outcome COGNITIVE ABILITIES COURSE OUTCOMES

Describe the key features and syntax of Python programming


1 REMEMBERING language.

Understand data types, control structures, and data structures in


2 UNDERSTANDING Python.

Apply Python programming skills to manipulate data and solve


3 APPLYING problems.

4 ANALYSING Analyze data using Python libraries such as Pandas and NumPy.

Evaluate machine learning algorithms and their performance using


5 EVALUATING Python.

Create and implement machine learning models for classification and


Reference Books
Lexicon MILE MBA@IICMR
• Machine Learning using Python - Manaranjan Pradhan, U Dinesh Kumar
• Ultimate Step by Step Guide to Machine Language using Python: Predictive
modeling concepts explained in simple terms for beginners by Daneyal Anis
• Machine Learning by Anuradha Srinivasaraghavan and Vincy Joseph
• Machine Learning in Python – Michael Bowles
MOOC’S Courses
Lexicon MILE
• Swayam -https://onlinecourses.nptel.ac.in/noc22_cs08/preview
• Cousera - https://www.coursera.org/learn/machine-learning-with-python
About Python
• Python was created by Guido van Rossum and first released in 1991. It
is a powerful language with efficient high-level data structures that
allow easy programming. It is a useful language for rapid development
of applications.
• Python is an interpreted, high-level, general-purpose programming
language that features a dynamic type system and automatic memory
management. It supports multiple programming paradigms, like
procedural and functional. Its popularity for data science is driven by
the ease of writing code and the availability of a comprehensive
library.
• Python interpreters are freely available for most of the commonly
used operating systems. CPython, the reference implementation of
Python, is open source software.
Why Python ?
Lexicon MILE

Python is Open Source which means its available free of cost.


Python is simple and so easy to learn
Python is versatile and can be used to create many different
things.
Python has powerful development libraries include AI, ML
etc.
Python is much in demand and ensures high salary
Why Python ?
Lexicon MILE

Python is Interpreted − Python is processed at runtime by the


interpreter. You do not need to compile your program before
executing it. This is similar to PERL and PHP.
Python is Interactive − You can actually sit at a Python prompt and
interact with the interpreter directly to write your programs.
Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
Python is a Beginner's Language − Python is a great language for the
beginner-level programmers and supports the development of a wide
range of applications from simple text processing to WWW browsers
to games.
Features of Python ?
Lexicon MILE

Easy to learn − Python has a simple structure, few keywords, and a


clear syntax. This makes it easy for the student to learn quickly. Code
written in Python is easier to read and understand.
Easy to maintain − The source code for Python is pretty easy to keep
up with.
A large standard library − Most of Python's library is easy to move
around and works on UNIX, Windows, Mac.
Portable − Python can run on a wide range of hardware platforms, and
all of them have the same interface.
First Python Programming
Lexicon MILE

print(“ This is my first Python Program.


Python is too easy”)
Getting the Input from the user
Lexicon MILE
Python Interpreter Works in interactive and scripted mode. Python
code is executed by one statement at a time method. Python
interpreter has two components. The translator checks the statement
for syntax. If found correct, it generates an intermediate byte code.
There is a Python virtual machine which then converts the byte code
in native binary and executes it. The following diagram illustrates the
mechanism:
Python Interpreter
Lexicon MILE

a = int(input("Enter value for a"))


b = int(input("Enter value for b"))
print("The number you have entered for a is ", a)
print("The number you have entered for b is ", b)
Syntax
Lexicon MILE

• Comments are indicated by the hash mark (pound sign) #. Any text preceded
by the sign # is ignored by the Python interpreter. This is often used to add
comments to code. At times you may also want to exclude certain blocks of
code without deleting them.
• Two statements on the same line are separated with a semicolon ";"
• No semicolon at the end of lines
• A long line continue on next with “\" (it is not always needed)
• Grouping is obtained through indentation
• One Python script is considered a module that can be run or imported by other
modules
• Assignment uses the equal sign "="
Python Indentations
• Python uses indentation to indicate a block of code
Example
a=5

b=2
if a > b:
print("a is greater than b!")

• Python will give you an error if you skip the indentation:


Example
a=5
b=2
if a > b:
print("a is greater than b!")
<Error>
Creating
Variables
• Python does not require declaration of variables
• As soon as you assign a value to a variable, it gets created
Example
name = "Anuj"
age = 36
print(name,age)

• Python automatically recognizes the data type based on the value that is assigned to a variable
Example
x=5 # x is of type int
x = "Hello" # x is now of type str
print(x) # This will print "Hello" as variable x has been assigned a new value
Python Numbers
• There are three numeric types in Python:
• int
• float
• complex
• Variables of numeric types are created automatically as soon as you assign a value to them:
Example
x = 23 # int
y = 5.6 # float
z = 1+2j # complex
• To identify the type of any variable or object in python, use the type() function
Python Operators
• Operators are used to perform different kinds of operations on variables and values
• Following is a list of operators in Python:
• Arithmetic operators (+,-,*,/,//,%,**)
• Assignment operators (=,+=,-= etc)
• Comparison operators (==,!=,>,<,>=,<=)
• Logical operators (and, or, not)
• Membership operators (in, not in)
• Identity operators (is, not is)
• Bitwise operators (works on bits)
Operators
Lexicon MILE
The following tokens are operators:

+ - * ** / // % @
<< >> & | ^ ~
< > <= >= == !=

Delimeters

The following tokens serve as delimiters in the grammar: ( ) [

] { }
, : . ; @ = ->
+= -= *= /= //= %= @=
&= |= ^= >>= <<= **=
Keywords
Lexicon MILE

The following identifiers are used as reserved words, or keywords of the language,
and cannot be used as ordinary identifiers. They must be spelled exactly as written here:

False class finally is return


True continue for lambda try
None def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Let us make a
calculator
Assignment
Lexicon MILE • Assignment creates references, not values:

tmp = "hello"; tmp = 10

# the first string will be deallocated

• Contrary to C, assignment do not have value:

y = (x = x + 1) is invalid

• As in C: x += 1 is valid

• Note that pre/post increment/decrement: x++; ++x; x--; --x are invalid

• Multiple assignment (references to a unique object): x=y=z=1

• Multiple assignments: (x,y,z)=(3.5, 5.5, 'string')

• Example of swaping variables value: (x,y) = (y,x)


Identifiers
Lexicon MILE

• First character: a letter or the symbol “_"


• Additional characters: alphanumeric or the symbol “_“
• Identifiers are unlimited in length
• They are case-sensitive
Variables
Lexicon MILE
• Variables are created when they are assigned.
• No declaration required.
• The variable name is case sensitive: ‘val’ is not the same as ‘Val’.
• The type of the variable is determined by Python.
• A variable can be reassigned to whatever, whenever required.
# this is an assignment statement
>>> type(a) # expression: outputs the value in interactive mode
<type 'str'>

>>> a = 'Hello world!' >>> n = 12


>>> print (a) >>> print
'Hello world!' (n)
12
>>> type(n)
<type 'int'>
>>> n = 12.0
>>> type(n)
<type 'float'>
Variable Types
Type Description
None The Python “null” value (only one instance of the None object exists)

str String type. ASCII-valued only in Python 2.x and Unicode in Python 3

unicode Unicode string type

float Double-precision (64-bit) floating point number. Note there is no separate


double type.

bool A Trueor Falsevalue

int Signed integer with maximum value determined by the platform

long Arbitrary precision signed integer. Large intvalues are automatically converted
to long.
Numbers
Lexicon MILE
The primary Python types for numbers are int and float. Python also supports other types of
numbers, such as Decimal and Fraction. In addition, Python has built-in support for complex
numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).
The Python interpreter acts as a simple calculator. You can type an expression at it and it will
writevalue. Expression syntax is straightforward: the operators +, -, * and / work just like in most
the
other languages (for example, Pascal or C); parentheses (()) can be used for grouping.
For example:
>>> 2 + 2
4
>>> 50 - 5*6 20
>>> (50 - 5*6) / 4
5.0
>>> 8/ 5 # division always returns a floating
point number
1.6
The integer numbers (e.g. 2, 4, 20) have type int, and numbers with a fractional part (e.g. 5.0,
1.6) have type float.
Numbers
 Division (/) always returns a float. To do floor division (mathematical division that rounds
down to nearest integer) and get an integer result (discarding any fractional result), you can use
Lexicon MILE
the // operator; to calculate the remainder you can use
%:
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
 With Python, it is possible to use the ** operator to calculate
powers.
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
 ** has higher precedence than -. Example: -3**2 will be interpreted as -(3**2)
and thus result in -9. To avoid this and get 9, you can use (-3)**2.
Numbers
The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before
the next interactive prompt:
>>> width = 20
>>> height = 5 * 9
>>> width * height
900

If a variable is not “defined” (assigned a value), trying to use it will give you an error:
>>> n # try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module> NameError:
name 'n' is not defined

There is full support for floating point; operators with mixed type operands convert the integer
operand to floating point:
>>> 3 * 3.75 / 1.5
7.5
Numbers

In interactive mode, the last printed expression is assigned to the variable _. This means that
when you are using Python as a desk it is somewhat easier to
calculator, calculations, for example: continue
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax 12.5625
>>> price + _ 113.0625
>>> round(_, 2)
113.06
Strings
MBA@IICMR
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They
can be enclosed in single quotes (’...’) or double quotes ("...") with the same result. \ can be used to
escape quotes:

>>> 'spam eggs‘ # single quotes


'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't“ # ...or use double quotes instead
"doesn't"
>>> '"Yes,"
he said.'
>>>
'"Yes,"
"\"Yes,\"
he he said."
said.'
'"Yes," he said.'
>>> '"Isn\'t," she
said.'
'"Isn\'t," she said.'
Strings
In the interactive interpreter, the output string is enclosed in quotes and special characters are
escaped with backslashes. While this might sometimes look different from the input (the enclosing
quotes could change), the two strings are equivalent. The string is enclosed in double quotes if the string
contains a single quote and no double quotes, otherwise it is enclosed in single quotes.
The print() function produces a more readable output, by omitting the enclosing quotes and
by printing escaped and special characters:

>>> '"Isn\'t," she said.'


'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond
line.'

# \n means newline
>>> s # without
print(), \n is included in the
output
'First line.\nSecond line.'
>>> print(s) # with
Strings

If you don’t want characters prefaced by \ to be interpreted as special


characters, you can use raw strings by adding an r before the first quote:
>>> print('C:\some\name') # here \n means newline!
C:\some ame
>>> print(r'C:\some\
name') # note the r before the quote
C:\some\name
Strings
String literals can span multiple lines. One way is using triple-quotes: ""“…….""" or
’’’…....’’’. End of lines are automatically included in the string, but it’s possible to
prevent this by adding a \ at the end of the line. The following example:

print("""\
Usage: thingy
[OPTIONS]
-h Display this usage
-H message Hostname to
hostname connect to
""")
produces the following output (note that the initial newline is not included):

Usage: thingy [OPTIONS]


-h Display this usage
-H hostname message Hostname to
connect to
Strings
Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
Two or more string literals (i.e. the ones enclosed between quotes) next to each other
are automatically concatenated.
>>> 'Py' 'thon'
'Python'
This only works with two literals though, not with
variables or expressions:
>>> prefix = 'Py'
>>> prefix 'thon'

# can't concatenate a variable and


a string literal
...
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
...
SyntaxError: invalid syntax
Strings
Following feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined
together.‘
Indexing & Slicing of Strings
Strings can be indexed (subscripted), with the first character having index 0. There is no
separate character type; a character is simply a string of size one:
>>> word = 'Python'
>>> # character in position 0
word[0] 'P'
>>> # character in position 5
word[5] 'n'
Indices may also be negative numbers, to start counting from the right:
>>> word[- # last character
1]
'n' # second-last character
>>> word[-
2] 'o'
>>> word[-#Note that since -0 is the same as 0, negative indices start from -1.
'P‘
Strings - Indexing & Slicing
In addition to indexing, slicing is also supported. While indexing is used to obtain
individual characters, slicing allows you to obtain substring:
>>> # characters from position 0 (included) to 2 (excluded)
word[0:2]
'Py' # characters from position 2 (included) to 5 (excluded)
>>> word[2:5]
Note that 'tho‘
the start is always included, and the end always excluded. This makes sure that
s[:i] + s[i:] is always equal to s:
>>> word[:2] + word[2:]
'Python‘
>>> word[:4] + word[4:]
'Python‘
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted
second index defaults to the size of the string being sliced.
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> # characters from position 4 (included) to the end
word[4:]
'on' # characters from the second-last (included) to the end
>>> word[-
2:]
Strings - Indexing & Slicing
One way to remember how slices work is to think of the indices as pointing between
characters, with the left edge of the first character numbered 0. Then the right edge of the
last character of a string of n characters has index n, for example:

| P | y
| t | h |
o | n 0 1
2 3
4 5 6
-6 -5 -4 -3
-2 -1

The first row of numbers gives the position of the indices 0...6 in the string; the
second row gives the corresponding negative indices.

For non-negative indices, the length of a slice is the difference of the indices, if both are
Strings - Indexing & Slicing
MBA@IICMR Attempting to use an index that is too large will result in an error:
>>> word[42] # the word only has 6
characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module> IndexError: string
index out of range
However, out of range slice indexes are handled gracefully when used for slicing:
>>> word[4:42]
'on'
>>> word[42:]
'‘
Python strings
cannot be changed
—they are
immutable.
Therefore, assigning
to an
indexed position in
the string results in
an error:
>>>
Strings
MBA@IICMR

If you need a different string, you should create a new one:


>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py' 'Pypy'

The built-in function len() returns the length of a string:


>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
Strings
Python built-in ring Methods
St
Argument Description
count Return the number of non-overlapping occurrences of substring in the string.
endswith, startswith Returns True if string ends with suffix (starts with prefix).
join Use string as delimiter for concatenating a sequence of other strings.
index Return position of first character in substring if found in the string. Raises
ValueError if not found.
find Return position of first character of first occurrence of substring in the string. Like
index, but returns -1 if not found.
rfind Return position of first character of last occurrence of substring in the string.
Returns -1 if not found.
replace Replace occurrences of string with another string.
strip, rstrip, lstrip Trim whitespace, including newlines; equivalent to x.strip() (and rstrip, lstrip,
respectively) for each element.
split Break string into list of substrings using passed delimiter.
lower, upper Convert alphabet characters to lowercase or uppercase, respectively.
ljust, rjust Left justify or right justify, respectively. Pad opposite side of string with spaces (or some
other fill character) to return a string with a minimum width.
Booleans
The two Boolean values in Python are written as True and False. Comparisons
other conditional expressions evaluate to either True or False. Boolean values are
and
combined with the and and or keywords:
In [001]: True and True Out[001]: True

In [002]: False or True


Out[002]: True

In [003]: a = [1, 2, 3]
.....: if a:
.....: print 'I found something!'
.....: In [005]: bool([]), bool([1, 2, 3])
I found something! Out[005]: (False, True)

In [004]: b = [] In [006]: bool('Hello world!'), bool('')


.....: if not b: Out[006]: (True, False)
.....: print 'Empty!'
.....: In [007]: bool(0), bool(1)
Empty! Out[007]: (False, True)
None
None is the Python null value type. If a function does not explicitly return a value,
it
implicitly returns None.
In [008]: a = None In
[009]: a is None Out[009]: True

In [010]: b = 5
In [011]: b is not None Out[011]:
True

None is also a common default value for


optional function arguments: def

add_and_maybe_multiply(a, b, c=None):
result = a + b

if c is not None:
result = result * c return result
Type casting
The str, bool, int and float types are also functions which can be used to
cast values to those types:

In [012]: s = '3.14159'

In [013]: fval = float(s)

In [014]: type(fval) Out[014]:


float

In [015]: int(fval)
Out[015]: 3

In [016]: bool(fval)
Out[016]:
True

In [017]: bool(0)
Out[017]: False
Dates and times
The built-in Python datetime module provides datetime, date, and time types. The
datetime type as you may imagine combines the information stored in date and time and is the
most commonly used:

In [018]: from datetime import datetime, date, time In [019]: dt

= datetime(2011, 10, 29, 20, 30, 21)


In [020]: dt.day In [328]: dt.minute
Out[020]: 29 Out[328]: 30

Given a datetime instance, you can extract the equivalent date and time objects
by calling methods on the datetime of the same name:

In [021]: dt.date()
Out[021]: datetime.date(2011, 10, 29)

In [022]: dt.time()
Out[022]: datetime.time(20, 30, 21)
Dates and times
The strftimemethod formats a datetimeas a string:

In [023]: dt.strftime('%m/%d/%Y %H:%M')


Out[023]: '10/29/2011 20:30'

Strings can be converted (parsed) into datetime


objects using the strptime
function:

In [024]: datetime.strptime('20091031', '%Y%m%d')


Out[024]: datetime.datetime(2009, 10, 31, 0, 0)

When aggregating or otherwise grouping time series data, it will occasionally be useful to
replace fields of a series of datetimes, for example replacing the minute and second fields
with zero, producing a new object:

In [025]: dt.replace(minute=0, second=0) Out[025]:


datetime.datetime(2011, 10, 29, 20, 0)
Dates and times
The difference of two datetimeobjects produces a datetime.timedeltatype:

In [026]: dt2 = datetime(2011, 11, 15, 22, 30)


In [027]: delta = dt2 - dt

In [028]: delta
Out[028]: datetime.timedelta(17, 7179)

In [029]: type(delta) Out[029]:


datetime.timedelta

Adding a timedeltato a datetimeproduces a new shifted datetime:

In [030]: dt
Out[030]: datetime.datetime(2011, 10, 29, 20, 30, 21) In
[031]: dt + delta
Out[031]: datetime.datetime(2011, 11, 15, 22, 30)
Datetime format specification
Type Description
%Y 4-digit year
%y 2-digit year
%m 2-digit month [01, 12]
%d 2-digit day [01, 31]
%H Hour (24-hour clock) [00, 23]
%I Hour (12-hour clock) [01, 12]
%M 2-digit minute [00, 59]
%S Second [00, 61] (seconds 60, 61 account for leap seconds)
%w Weekday as integer [0 (Sunday), 6]
%U Week number of the year [00, 53]. Sunday is considered the first day of the week, and days before the
first Sunday of the year are “week 0”.
%W Week number of the year [00, 53]. Monday is considered the first day of the week, and days before the
first Monday of the year are “week 0”.
%z UTC time zone offset as +HHMM or -HHMM, empty if time zone naive
%F Shortcut for %Y-%m-%d, for example 2012-4-18
%D Shortcut for %m/%d/%y, for example 04/18/12
if
Control Flow
Statements
The if statement is one of the most well-known control flow statement types. It checks
a condition which, if True, evaluates the code in the block that
follows.
>>> x = int(input("Please enter an integer:
")) Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More blocks. The keyword
‘elif‘ is short can
An if statement for be
‘else if’, and
optionally is useful
followed to avoid
by one or excessive indentation. An if ...
more elif
elif ... elif ... sequence is a substitute for the switch or case statements
found in other languages.
Control Flow
for
Statements
Python’s for statement iterates over the items of any sequence (a list or a string), in the
order that they appear in the sequence. For example
>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
If you need to modify the sequence you are iterating over while inside the loop (for
example to duplicate selected items), it is recommended that you first make a copy.
Iterating over a sequence does not implicitly make a copy. The slice notation makes this
especially convenient:
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
Control Flow
while
loop
A while loop specifies a condition and a block of code that is to be executed until the
condition evaluates to False or the loop is explicitly ended with break:
x = 256 >>> # Fibonacci series:
total = 0 ... # the sum of two elements defines the next
while x > ... a, b = 0, 1
0: >>> while b <
if total > 10:
500: break ... print(b)
total += x ... a, b = b, a+b
x = x // 2 ...
1
1
2
3
5
8
Control Flow
break and continue Statements, and else Clauses on
Loops
The break statement, like in C, breaks out of the smallest enclosing for or while loop.
Loop statements may have an else clause; it is executed when the loop terminates
through exhaustion of the list (with for) or when the condition becomes false (with while),
but not when the loop is terminated by a break statement. This is exemplified by the
following loop, which searches for prime numbers:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else: #the else clause belongs to the for loop, not the if statement
... # loop fell through without finding a factor
... print(n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Control Flow
break and continue Statements, and else Clauses on
Loops
When used with a loop, the else clause has more in common with the else clause
of a try statement than it does that of if statements: a try statement’s else
clause runs when no exception occurs, and a loop’s else clause runs when no break
occurs.
The continue statement, also borrowed from C, continues with the next iteration of
the loop:
>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found a number", num)
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
Control Flow
pass
Statements
The pass statement does nothing. It can be used when a statement is
required syntactically but the program requires no action. For example:
if x < 0:
print
'negative!' elif x ==
0:
# TODO: put something
smart here pass
else:
print 'positive!'

It’s common to use pass as a place-holder in code while working on a new


piece of functionality:

def f(x, y, z):


# TODO: implement this
function! Pass
Control Flow
The range()Function
If you do need to iterate over a
sequence of numbers, the built-in
function range() comes in handy. It
generates arithmetic progressions:

>>> for i in range(5):


... print(i) To iterate over the indices of a sequence, you
... can combine range() and len() as
0 follows:
1 >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
2 >>> for i in range(len(a)):
3 ... print(i, a[i])
4 ...
1 Mary
2 had
3a
4 little
5 lamb
Control Flow
The range()
Function
The range function produces a list of evenly-spaced integers:
In [032]: range(10)
Out[032]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Both a start, end, and step can be given:


In [033]: range(0, 20, 2)
Out[033]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

As you can see, range produces integers up to but not including the endpoint. A
common use of range is for iterating through sequences by index:
seq = [1, 2, 3, 4]
for i in range(len(seq)):
val = seq[i]
Control Flow
The xrange()
Function
For very long ranges, it’s recommended to use xrange, which takes the same
arguments as range but returns an iterator that generates integers one by one rather than
generating all of them up-front and storing them in a (potentially very large) list. This
snippet sums all numbers from 0 to 100 that are multiples of 3 or 5:

sum = 0
for i in xrange(100):
# % is the modulo
operator if x % 3 == 0 or x
% 5 == 0: sum += i

In Python 3, range always returns an iterator, and thus it is not necessary to use the
xrange function.
Data Structures and Sequences

Tuple
A tuple is a one-dimensional, fixed-length, immutable sequence of Python objects. The
easiest way to create one is with a comma-separated sequence of values:

In [038]: tup = 4, 5, 6
In [039]: tup
Out[039]: (4, 5, 6)

When defining tuples in more complicated expressions, it’s often necessary to enclose the
values in parentheses, as in this example of creating a tuple of tuples:

In [040]: nested_tup = (4, 5, 6), (7, 8)


In [041]: nested_tup
Out[041]: ((4, 5, 6), (7, 8))
Data Structures and Sequences
Tuple
Any sequence or iterator can be converted to a tuple by invoking tuple:

In [042]: tuple([4, 0, 2])


Out[042]: (4, 0, 2)
In [043]: tup = tuple('string') In [044]:
tup
Out[044]: ('s', 't', 'r', 'i', 'n', 'g')

Elements can be accessed with square brackets [] as with most other sequence types. Like
C, C++, Java, and many other languages, sequences are 0-indexed in Python:

In [045]: tup[0]
Out[045]: 's'
Tuple
Data Structures and Sequences
While the objects stored in a tuple may be mutable themselves, once created it’s not
possible to modify which object is stored in each slot:
In [046]: tup = tuple(['foo', [1, 2], True])
In [047]: tup[2] = False
--------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-365-c7308343b841> in <module>()
----> 1 tup[2] = False
TypeError: 'tuple' object does not support item assignment

# however
In [048]: tup[1].append(3)
In [049]: tup
Out[049]: ('foo', [1, 2, 3], True)
Tuples can be concatenated using the + operator to produce longer tuples:
In [050]: (4, None, 'foo') + (6, 0) + ('bar',)
Out[050]: (4, None, 'foo', 6, 0, 'bar')
Data Structures and Sequences
Tuple
Multiplying a tuple by an integer, as with lists, has the effect of concatenating together
that many copies of the tuple.
In [051]: ('foo', 'bar') * 4
Out[051]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')
Note that the objects themselves are not copied, only the references to them.

Unpacking tuples
If you try to assign to a tuple-like expression of variables, Python will attempt to unpack
the value on the right-hand side of the equals sign:
In [052]: tup = (4, 5, 6)
In [053]: a, b, c = tup In [054]:
b
Out[054]: 5
Data Structures and Sequences

Unpacking tuples
Even sequences with nested tuples can be unpacked:
In [055]: tup = 4, 5, (6, 7)
In [056]: a, b, (c, d) = tup In [057]:
d Out[057]: 7
Using this functionality it’s easy to swap variable names, a task which in many languages might
look like:
tmp = a
a=b
b=
tmp b,
a = a, b
One of the most
common uses of
Data Structures and Sequences

Tuple methods

Since the size and contents of a tuple cannot be modified, it is very light on instance methods. One
particularly useful one (also available on lists) is count, which counts the number of occurrences of a
value:

In [058]: a = (1, 2, 2, 2, 3, 4, 2)
In [059]: a.count(2)
Out[059]: 4
Data Structures and Sequences
List
Python has a number of compound data types, used to group together other values. The most versatile is
the list, which can be written as a list of comma-separated values (items) between square brackets.
Lists might contain items of different types, but usually the items all have the same type. They can be
defined using square brackets [] or using the list type function:

In [060]: a_list = [2, 3, 7, None] In [061]: tup = ('foo',


'bar', 'baz')

In [062]: b_list = list(tup) In [381]: b_list


Out[062]: ['foo', 'bar', 'baz']
In [063]: b_list[1] = 'peekaboo' In [383]: b_list
Out[063]: ['foo',
'peekaboo', 'baz']
Lists and tuples are semantically similar as one-dimensional sequences of objects and thus can be used
interchangeably in many functions.
Data Structures and Sequences

List
Adding and removing elements

Elements can be appended to the end of the list with the append method:

In [064]: b_list.append('dwarf')
In [065]: b_list
Out[065]: ['foo', 'peekaboo', 'baz', 'dwarf']

Using insert you can insert an element at a specific location in the list:

In [066]: b_list.insert(1, 'red')


In [067]: b_list
Out[067]: ['foo', 'red',
'peekaboo', 'baz', 'dwarf']

Note: insert is computationally expensive compared with append as references


to subsequent elements have to be shifted internally to make room for the new element.
Data Structures and Sequences
List
MBA@IICMR
Adding and removing elements
The inverse operation to insert is pop, which removes and returns an element at a
particular index:
In [068]: b_list.pop(2) Out[068]:
'peekaboo' In [069]: b_list
Out[069]: ['foo', 'red', 'baz', 'dwarf']
Elements can be removed by value using remove, which locates the first such value and removes it from
the last:
In [070]: b_list.append('foo')
In [071]: b_list.remove('foo')
In [072]: b_list
Out[072]: ['red', 'baz', 'dwarf', 'foo']
If performance is not a concern, by using append and remove, a Python list can be used as a
perfectly suitable “multi-set” data structure. You can check if a list contains a value using the in
keyword:
In [073]: 'dwarf' in b_list
Out[073]: True
List
Data Structures and Sequences
MBA@IICMR Concatenating and combining lists
Similar to tuples, adding two lists together with + concatenates them:
In [074]: [4, None, 'foo'] + [7, 8, (2, 3)]
Out[074]: [4, None, 'foo', 7, 8, (2, 3)]
If you have a list already defined, you can append multiple elements to it using
the extend method:
In [075]: x = [4, None, 'foo']
In [076]: x.extend([7, 8, (2, 3)])
In [077]: x
Out[077]: [4, None, 'foo', 7, 8, (2, 3)]
Note that list concatenation is a compartively expensive operation since a new list be created and
the
mustobjects copied over. Using extend to append elements to an existing list, especially if you are
building up a large list, is usually preferable. Thus,
everything = []
for chunk in list_of_lists:
everything.extend(chunk)
is faster than than the concatenative alternative
everything = []
for chunk in list_of_lists:
everything = everything + chunk
Data Structures and Sequences
MBA@IICMR

List
Sorting

A list can be sorted in-place (without creating a new object) by calling its sort function:
In [078]: a = [7, 2, 5, 1, 3]
In [079]: a.sort()
In [080]: a
Out[080]: [1, 2, 3, 5, 7]

sort has a few options that will occasionally come in handy. One is the ability to pass a secondary
sort key, i.e. a function that produces a value to use to sort the objects. For example, we could sort a
collection of strings by their lengths:

In [081]: b = ['saw', 'small', 'He', 'foxes', 'six'] In [082]:


b.sort(key=len
) In [083]: b
Out[083]:
['He', 'saw',
'six', 'small',
'foxes']
List
Data Structures and Sequences
MBA@IICMR
Binary search and maintaining a sorted list

The built-in bisect module implements binary-search and insertion into a sorted list.
bisect.bisect finds the location where an element should be inserted to keep it sorted, while
bisect.insort actually inserts the element into that location:

In [084]: import bisect


In [085]: c = [1, 2, 2, 2, 3, 4, 7]
In [086]: bisect.bisect(c, 2) In [407]: bisect.bisect(c, 5)
Out[086]: 4 Out[407]: 6

In [087]: bisect.insort(c, 6)
In [088]: c
Out[088]: [1, 2, 2, 2, 3, 4, 6, 7]

Note: The bisect module functions do not check whether the list is sorted as doing so would be
computationally expensive. Thus, using them with an unsorted list will succeed without error but may lead
to incorrect results.
Data Structures and Sequences
MBA@IICMR

List
Slicing

You can select sections of list-like types (arrays, tuples, NumPy arrays) by using slice
notation, which in its basic form consists of start:stop passed to the indexing
operator []:

In [089]: seq = [7, 2, 3, 7, 5, 6, 0, 1]


In [089]: seq[1:5]
Out[089]: [2, 3, 7, 5]

Slices can also be assigned to with a sequence:


In [090]: seq[3:4] = [6, 3]
In [091]: seq
Out[091]: [7, 2, 3, 6, 3, 5, 6, 0, 1]
Data Structures and Sequences
MBA@IICMR
List
Slicing

While element at the start index is included, the stop index is not included, so that the number of elements in
the result is stop - start. Either the start or stop can be omitted in which case they default to the
start of the sequence and the end of the sequence, respectively:

In [092]: seq[:5]
Out[092]: [7, 2, 3, 6, 3]
In [093]: seq[3:]
Out[093]: [6, 3, 5, 6, 0, 1]

Negative indices slice the sequence relative to the end:


In [094]: seq[-4:]
Out[094]: [5, 6, 0, 1]
In [095]: seq[-6:-2]
Out[095]: [6, 3, 5, 6]
Data Structures and Sequences
List
MBA@IICMR
enumerate
It’s common when iterating over a sequence to want to keep track of the index of the current item. A
do-it-yourself approach would look like:
i=0
for value in collection:
# do something with value i += 1
Since this is so common, Python has a built-in function enumerate which returns a sequence of
(i, value) tuples:
for i, value in enumerate(collection):
# do something with
value
When indexing data, a useful pattern that uses enumerate is
computing a dict
mapping the values of a sequence (which are assumed to be
unique) to their locations in the sequence:
In [096]: some_list = ['foo', 'bar', 'baz']
In [098]: mapping
In [097]: mapping
Out[098]:= {'bar':
dict((v,1,i) 'baz':
for i, v2,in'foo':
enumerate(some_list))
0}
Data Structures and Sequences
MBA@IICMR

List
sorted

The sorted function returns a new sorted list from the elements of any sequence:

In [099]: sorted([7, 1, 2, 6, 0, 3, 2])


Out[099]: [0, 1, 2, 2, 3, 6, 7]

In [100]: sorted('horse race')


Out[100]: [' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']

A common pattern for getting a sorted list of the unique elements in a sequence is to
combine sorted with set:
In [101]: sorted(set('this is just some string'))
Out[101]: [' ', 'e', 'g', 'h', 'i', 'j', 'm', 'n', 'o', 'r', 's', 't',
'u']
Data Structures and Sequences
MBA@IICMR

List
zip
zip “pairs” up the elements of a number of lists, tuples, or other sequences, to
create a list of tuples:
In [102]: seq1 = ['foo', 'bar', 'baz']
In [103]: seq2 = ['one', 'two', 'three'] In [104]:
zip(seq1, seq2)
Out[104]: [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
zip can take an arbitrary number of sequences, and the number of elements it
produces
is determined by the shortest sequence: In [105]:
seq3 = [False, True] In [106]: zip(seq1,
seq2, seq3)
Out[106]: [('foo', 'one', False), ('bar', 'two', True)]
List Methods
The list data type has many methods. Here are all of the methods of list
objects:
MBA@IICMR
List Methods Description
list.append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x].
list.extend(L) Extend the list by appending all the items in the given list. Equivalent to
a[len(a):] = L.
list.insert(i, x) Insert an item at a given position. The first argument is the index of the
element before which to insert, so a.insert(0, x) inserts at the front
of the list, and a.insert(len(a), x) is equivalent to a.append(x).

list.remove(x) Remove the first item from the list whose value is x. It is an error if there
is no such item.

list.pop([i ]) Remove the item at the given position in the list, and return it. If no index is
specified, a.pop() removes and returns the last item in the list. (The
square brackets around the i in the method signature denote that the
parameter is optional, not that you should type square brackets at that
position.)

list.clear() Remove all items from the list. Equivalent to del a[:].
List Methods
MBA@IICMR

List Methods Description

list.index(x) Return the index in the list of the first item whose value is x. It is an
error if there is no such item.

list.count(x) Return the number of times x appears in the list.

list.sort Sort the items of the list in place (the arguments can be used for sort
(key=None, customization, see sorted() for their explanation).
reverse=False
)
list.reverse() Reverse the elements of the list in place.

list.copy() Return a shallow copy of the list. Equivalent to a[:].


Data Structures and Sequences
MBA@IICMR
List Methods

Example continued…

>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>>
a.pop()
1234.5
>>> a
[-1, 1,
66.25, 333,
333]

Note: Methods like insert, remove or sort that only modify the list have
The Del Statement
MBA@IICMR
There is a way to remove an item from a list given its index instead of its value: the del statement. This
differs from the pop() method which returns a value. The del statement can also be used to remove
slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice).
For example:

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]


>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
del can also be used to delete entire variables:
>>> del a

Referencing the name a hereafter is an error (at least until another value is assigned to it).
Tuple
MBA@IICMR

Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Dictionaries
MBA@IICMR
Another useful data type built into Python is the dictionary. Dictionaries are indexed
by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be
used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object
either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be
modified in place using index assignments, slice assignments, or methods like append() and
extend().

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that
the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}.
Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the
dictionary; this is also the way dictionaries
are written on output.

The main operations on a dictionary are storing a value with some key and extracting the value
given the key. It is also possible to delete a key:value pair with del. If you store using a key that is
already in use, the old value associated with that key is forgotten. It is an error to extract a value
using a non-existent key.
MBA@IICMR
Dictionaries
Performing list(d.keys()) on a dictionary returns a list of all the keys used in the
dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys()) instead).
To check whether a single key is in the dictionary, use the in keyword. Here is a small example
using a dictionary:

>>> tel = {'jack': 4098, 'sape': 4139}


>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del
tel['sape']
>>> tel['irv']
= 4127
>>> tel
{'[g'uri ivd'o,
Dictionary
MBA@IICMR

thisdict = dict(name = "John", age = 36,


country = "Norway")
print(thisdict)

thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
Looping Techniques
MBA@IICMR

When looping through dictionaries, the key and corresponding value can be retrieved at the
same time using the items() method.

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}


>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure robin
the brave
When looping through a sequence, the position index and corresponding value can
be retrieved at the same time using the enumerate() function.

>>> for i, v in enumerate(['tic', 'tac', 'toe']):


... print(i, v)
...
1 tic
2 tac
3 toe
www.marsiantech.com
EXPERIENCE
PERFECTION

MBA@IICMR

You might also like