Introduction to Python

Setup

Overview

Time: 0 min
Objectives
  • Understand Google CoLab

To participate in this workshop, you will need to understand how to work with google colab You will also need to download the code file used for this workshop and import it to your coLab.

Code Files

Workshop Code:

Click below to access the notebook to follow along with the workshop:

Python Workshop part 1

Python Workshop part 2

Practice Code:

Remember to save a copy in your drive!(Click on File -> Save a copy in Drive)

Worksheet 1

Worksheet 2

Solution:

Solution for Practice code -

Software setup

We will be using Google CoLab for this workshop. Check the video below to understand how to access and use google coLab.

Using Google CoLab

Key Points


Introduction

Overview

Time: 0 min
Objectives

What is Python?

According the python.org, Python is an interpreted, object-oriented, high-level programming language that has a simple and easy to learn syntax. It has high level built in data structures with dynamic typing and dynamic binding, which make it attractive for application developement. However, the most know use of python is for data science. This is because it provides functionalities to deal with statistical aspects of data science. It also provides libraries to deal with machine learning models.

Why Python?

Python is easy to pick up. It has various in-built libraries that can be used to save time and effort. It is also very versatile, efficient and fast.

Who developed Python?

Python was created by Guido van Rossum, and first released on February 20, 1991. He stated his goals for python as

Domains where Python is used-

Applications where Python is used-

Key Points


Variables

Overview

Time: 0 min
Objectives
  • To understand variables and the rules of naming variables

What are Variables?

In python, we have constants and variables. A constant does not change its value over time. A variable is a container that is used to store values. The values of the variable can change in accordance to the manipulation being done on it.

How to create a Variable?

A variable is created the moment you assign a value to it.


name = "Recipe for pancakes"
no_of_ingredients = 5
print(no_of_ingredients)
print(name)

In python, we can also change the values assigned to the variables, without any restrictions.

no_of_ingredients = "Recipe for pancakes"    # x is now of type string
name = 5                       # name is now of type int
print(x)
print(name)

Policy for naming Variables

Variables in python should follow the given rules

Key Points


Inputs and Outputs

Overview

Time: 0 min
Objectives

Collect user input

The ‘input’ method provides an easy way to collect user input.


name= input('What is your name?')

In python, all inputs are stored as strings. You can change the data type using type casting


number = input("Enter a number")
number= int(number)

Output in Python

You can display output using python

print("Hello!")
name = "Digital Scholarship Hub"

print(f'Hello {name}! How are you?')

Using % Operator

We can use ‘%’ operator. % values are replaced with zero or more value of elements.

num = int(input("Enter a value: "))

print("The number is %d" %num)

Comments in Python:

Comments are texts starting with ‘#’ that are sprinked in the code. Python ignores these sentences while compilation. Comments are a great tool to -

r = int(input())
v= (4/3)*(3.14)*(r**3)
print(v)

r = int(input()) #reading input, i.e radius
v= (4/3)*(3.14)*(r**3) #calculate volume
print(v) #print volume

Multiline comments

To comments multiple lines, we can insert a # at the beginning of every line

# this is
# to demonstrate
# multiline comments in python
i=0
print(i)

Or, we can use a multiline string. We is triple quotes(‘’’ ……. ‘’’) for multiline string comments

'''
the following loop prints 
the value of the variable i, 
as long as it is less that 5
'''
i=0
while i<6:
 print(i)
 i=i+1

Key Points


Datatypes

Overview

Time: 0 min
Objectives
  • Learn about data types in python

What is a data type?

Data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected.

Python has the following data types built-in by default, in these categories:

Numeric Types: int, float, complex

Sequence Types: list, tuple, strings

Mapping Type: dict

Set Types: set

Boolean Type: bool

Numeric type:

There are 3 numeric types-

INT: These are variables that hold integer values(…-3,-2,-1,0,1,2,3…)

FLOAT: This data type is used to represent real number with floating point representation. It is specified by a decimal point.

COMPLEX: An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the .real and imaginary part can be represented by .imag .

Sequence type:

In Python, sequence is an ordered collection of various object. These objects could be of similar or different data types.

Lists: List is an ordered collection of data. It is declared using “[]”

Tuples: Tuples are immutable lists i.e. tuples cannot be modified after it is created. It is declared using “()”

Strings: Strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote or triple quote.

Mapping type:

Maps are a data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In python, we use dictionary (dict) data type as a map.

Dict: Dictionary holds key:value pairs. Each key:value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’. A Dictionary can be created by placing a sequence of elements within curly {} braces.Dictionary can also be created by the built-in function dict().

NOTE: Keys cannot be repeated in dictionary.

each key must be a unique value, but it is ok if values are repeated.

Set type:

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements.Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’. Type of elements in a set need not be the same.

Boolean Type:

Boolean (bool) is a data type with one of the two built-in values, True or False.\

Note –

True and False with capital ‘T’ and ‘F’ are the only valid booleans.

Get the data type of a variable:

We can use the type() function to understand the data type of the variable

x="recipe for pancakes"
type(x)

Type Casting

Type casting is a technique used to convert the data type of the value. In python you can use the following functions to convert the data types:

a=str(13456)
b=int("1234")
c=float(55)

Key Points


Operations

Overview

Time: 0 min
Objectives
  • Learn about basic operations in python

Operations in python

Operators are used to perform modifications on variables. We have different types of operations in python

Arithmetic Operators

These operators are used to perform basic mathematic operations on variables of numeric type.

x=3
y=4

print(x+y) # print the sum of x and y
print(x-y) # subtraction
print(x*y) # multiplication
print(x/y) #simple division
print(x**y) # print exponential product(x to the power of y)
print(x//y) # floor division
print(x%y) #modulus

Assignment Operators

Assignment operators are used to assign values to variables.

x = 6	
x += 2	#similar to x = x + 2	
x -= 2		#similar to x = x - 2	
x *= 2		#similar to x = x * 2	
x /= 2		#similar to x = x / 2	
x %= 2		#similar to x = x % 2	
x //= 2	#similar to x = x // 2	
x **= 2	  #similar to x = x ** 2	
x &= 2	  #similar to x = x & 2	
x |= 2	  #similar to x = x | 2	
x ^= 2	  #similar to x = x ^ 2	
x >>= 2	  #similar to x = x >> 2	
x <<= 2	  #similar to x = x << 2

Comparision Operators

Comparision operators are used to compare two values.

x=5
y=5
print(x == y)	# checks if x is equal to y
print(x != y) # checks if x is not equal to y	
print(x > y) # checks if x is greater than y	
print(x < y) # checks if x is less than y	
print(x >= y) # checks if x is greater that or equal to y
print(x <= y) # checks if x is less than or equal to y.

Note: In all of the above cases, the result is of boolean type(True or False)

Logical Operators

Logical operators are used to perform operations on conditional statements.

x=5
y=10
print(x==5 and y==10) # performs AND operation on the results of the comparision operators(True and True)
print(x==5 or y!=10) # performs OR operation on the results of the comparision operators(True or False)
print(not(x==5)) # reverses the result of the comparision operation.

Identity Operators

Identity operators are used to check if the objects are same based on the memeory allocation-

x=[1,2,3]
y=[1,2,3]
z=y
print(z is y) # returns True as z is the same object as y
print(z is x) # returns False as z is not the same object as x
print(z is not x) # returns True as z is not the same object as x

Membership Operators

Membership operators are used to check if a sequence is present in an object

x=["Digital","Scholorship","Hub"]
print("Hub" in x)# prints True, as "Hub" is in list x
print(2 not in x)# prints True, as 2 is not in list x

Key Points


Conditional Statements and Flow control

Overview

Time: 0 min
Objectives
  • Understand Conditional Statements and Control Flow statements

Indentation

Unlike other programming languages that use brackets to indicate blocks of code, in python we use indentation.

for i in range(0,5):
  print(i**2)
  print(i)
  print('In for loop')
  print("block")

Python will raise an error if you skip indentation. The number of spaces for each block is up to the programmer. However, it should be atleast one space and the number of spaces should be consistent through out that bloack of code.

Conditional Statements

Conditional statements provide us the ability to test a variable against a value and act in one way if the condition is met by the variable or another way if not.

If Statement

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.

pantry= ['flour',' baking powder','salt','white sugar','milk','egg','butter']

if 'baking powder' in pantry:
  print('We have everything needed to make pancakes!')
else:
  print("You can eat toast!")

if 'syrup' in pantry:
  print('Syrup is optional')
print('Done')

Elif Statement

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE

pantry= ['flour',' baking powder','salt','white sugar','milk','egg','butter']


if 'milk' in pantry:
  print('We have milk')
elif 'non-dairy milk' in pantry:
  print('Non-Dairy milk is available. We can make vegan pancakes if we have Apple Cider vinegar and vanilla instead of eggs and butter')
else:
  print("Incomplete ingredients")

Control Flow

Control Flow is the order in which instructions and function calls are implemented. Thw while and for loop provide us a way to manipulate control Flow

While Loop

The while loop is used to execute a set of statements as long as a condition is true.

butter_in_pantry = 12
servings=0
while butter_in_pantry > 0:
  servings=servings+8
  butter_in_pantry=butter_in_pantry-3
print("Servings:" + str(servings))

We use the BREAK and CONTINUE statements to control the flow of the loop

Break is used to exit the loop

butter_in_pantry = 150
servings=0
while butter_in_pantry > 0:
  servings=servings+8
  if servings >= 24:
    break
  butter_in_pantry=butter_in_pantry-3

Continue is used to stop the current iteration, and continue with the next

label_index = 0
while label_index <= 10:
  label_index += 1
  if label_index == 3:
    continue
  print("Label "+str(label_index))

For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

pantry= ['flour',' baking powder','salt','white sugar','milk','egg','butter','cheese','yeast','vanilla extract','oregano','maple syrup','strawberry jam']
for x in ingredients:
  print(x)
  print(" ")

The for loop also has break and continue operators like the while loop

Key Points


Functions

Overview

Time: 0 min
Objectives
  • To understand how to use functions in python.

What are Functions?

Functions are a set of instructions(code) bundled together to achieve a specific outcome.

Why do we need Functions?

One of the main advantages of functions is that it encourages code reusability. Instead of writing the lines of code at multiple places, we can write it as a fucntion and call it when needed.

How to use functions in python?

In Python a function is defined using the def keyword:

def my_function():
  print("Hello from a function")

How to call functions in python?

To call a function, use the function name followed by parenthesis:

def my_function():
  print("Hello from a function")

my_function()

What are arguments?

We can pass data to our functions, so that it can use it for computations.This data being passed is called an argument. You specify the value of an argument when you call the function.

def my_function(fname):
  print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")

LAMBDAS

A lambda function has no name when defining it, and it is contained in one line of code. We used the keyword LAMBDA to declare it.

x = lambda a : a + 10
print(x(5))

Key Points


Lists

Overview

Time: 0 min
Objectives
  • To understand how to work with lists

What are Lists?

Lists are just like the arrays, declared in other languages which is a ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.

Creating a List

Lists in Python can be created by just placing the sequence inside the square brackets[].

Accessing elements of a List

In order to access the list items, we use the index number. For example- we can access the third element by typing list[2]. In Python, negative sequence indexes represent positions from the end of the array. That is -1 refers to the last item, -2 refers to the second-last item, etc.

Note:

Indexing in python always starts from 0. In the below example, the index of 12 is 0.

pantry= ['flour',' baking powder','salt','white sugar','milk','egg','butter','cheese','yeast','vanilla extract','oregano','maple syrup','strawberry jam']


print(pantry)        #Prints list

print(pantry[0])     #Prints 1st element in the list

print(pantry[1:5])   #Prints 2nd to 5th elements in the list

print(pantry[4:])    #Prints all the elements after 4

print(pantry[-1])    #Prints the last element

Removing elements from a List

We use the inbuilt methods del(), pop(),remove().

pantry_1= ['flour',' baking powder','salt','white sugar','milk','egg','butter','cheese','yeast','vanilla extract','oregano','maple syrup','strawberry jam']


del(pantry_1[4])         #Delets the 5th element in the list 
print(pantry_1)  

pantry_1.remove('milk')      #Removes the element 22. Remove command removes the first matching element
print(pantry_1)

pantry_1.pop(1)          #Pops the element present on the first index
print(pantry_1)

Other Operations with list

pantry= ['flour',' baking powder','salt','white sugar','milk','egg','butter','cheese','yeast','vanilla extract','oregano','maple syrup','strawberry jam']
new_items=['pepper','yeast','cinnamon','chilli flakes','oregano']

print(len(pantry))     # The len() function is used to get the length of the list
updated_pantry= pantry + new_items     #Adding/Concatenating two lists
print(updated_pantry)
print(updated_pantry[-3:])     #Prints last 3 elements
#Checking if an element is present in the list

'pepper' in updated_pantry   #The 'in' statement checks whether certain element is present in the list or not. If the element is present it returns True else False
'yeast' in updated_pantry
'salt' not in updated_pantry #Similar to 'in' statement
#Min Max Function
lis6=[1,34,73,67,99,22,3,4]

print(min(lis6))
print(max(lis6))
#Adding value to the list
lis6.append(56)     #Appends only one element at the end of the list
print(lis6)
lis6.extend([11,2,33]) #Appends multiple elements at the end of the list
print(lis6)
updated_pantry.append('olive oil')
updated_pantry.extend(['Mayonnaise','Cream Cheese','Honey'])
lis6.insert(0,90)   #Inserts element at the mentioned index
print(lis6)
#Sorting elements in a list

lis7=[-6,1,-1,-5,3,6,7,8,22,5,4]

lis7.sort()         #Sorts elements in ascending order

print(lis7)

#Sorted function

lis8=[-3,11,2,3,8,4,1,5,-5,-8,-10,10]

print(sorted(lis8)) #Prints the sorted list

print(lis8)         #Prints the original list. Sorted function doesnot change the original list whereas sort function does
#Reassigning a list
print(lis8)

lis8[0]=100         #Assigns index 0 as 100

print(lis8)

Applications of List

Key Points


Tuples

Overview

Time: 0 min
Objectives
  • To understand the concept of Tuples in python

What is a Tuple?

Tuple is also an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is created.

Creating Tuple

In Python, tuples are created by placing a sequence of values separated by ‘comma’ in (). Tuples can contain any number of elements and of any datatype.

Note: Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.

Accessing elements of Tuple

In order to access the tuple items refer to the index number.The index must be an integer. Nested tuples are accessed using nested indexing.

#Tuples
#Unlike list tuples are enclosed in ()
#Cannot use store/stored functions for tuples because they are immutable

address=(750, 'South Halsted Street', 'Chicago', 60607)

print(address)

print(address[0])

print(address[1:3])

Applications of Tuples

Key Points


Dictionaries

Overview

Time: 0 min
Objectives
  • To understand the concept of dictionaries

What are Dictionaries?

A dictionary is a datatype that stores data in key value pairs. The key and value have a colon between them(:) and the key:value pairs are seperated by “,” It is ordered*, changeable and do not allow duplicates.

Creating a Dictionary

We create a dictionary using curly({}) brackets.

pantry={'eggs':10,'butter(tbsp)':20,'sugar(oz)':30,'salt(oz)':40}

print(pantry)

Accessing elements in a Dictionary

We can access elements using the key:

print(pantry['eggs'])

keys(): The keys method returns a list of all the keys in a dictionary

k= pantry.keys()
print(k)

Inserting/changing items in a Dictionary:

We can change items using the key. There is also an update() methos that takes a dictionary as an argument to change the values.

pantry['eggs']=50
pantry.update({'eggs':10})

We can also add new elements to a dictionary in the same way:

pantry['Flour']=50
pantry.update({'pepper':60,'olive oil':70})

Copying a Dictionary:

We can use the copy() method and the dict() function. We cannot simply do dictionary1=dictionary2 as it will only create a reference instead of copying the dictionary.

pantry_copy_1=pantry.copy()
pantry_copy_2=dict(pantry)

Deletion in Dictionaries:

We use the pop() method and the del keyword to delete elements in dictionaries. We pass the key as an argument to indicate the value to be deleted

pantry.pop('olive oil')
del pantry['pepper']

the del keyword can delete the dictionary completely:

del pantry
print(pantry)

The clear() method is used to empty the dictionary.

pantry.clear()

The concept of nested Dictionaries:

A dictionary can dictionaries within itself. This is called a nested dictionary:

pantry = {
  "Pancakes" : {
    "Flour" : 6,
    "milk" : 2,
    "Eggs": 3
    "Butter" : 6
  },
  "French_toast" : {
    "Bread" : "2",
    "Eggs" : 2,
    "Sugar" : 3
    "milk" :1
  },
}
# or we can also do the following:

Pancakes = {
    "Flour" : 6,
    "milk" : 2,
    "Eggs": 3
    "Butter" : 6
  }
French_toast = {
    "Bread" : "2",
    "Eggs" : 2,
    "Sugar" : 3
    "milk" :1
  }
pantry_2 = {
  "Pancakes" : letters,
  "French_toast" : capital_letters
}

Applications of Dictionaries

Key Points


Sets

Overview

Time: 0 min
Objectives
  • Understand how sets work in python

What are sets?

A set is a collection which is unordered and unindexed.

Creating a set:

Sets are created using curly brackets.

set_example={"Digital","Scholorship","Hub",2}
print(set_example)

Remember-

Set items are -

  • unordered: We cannot be sure which order the element are stored and displayed in.
  • unchangeable: We cannot update the elements of the set. However, we can insert and delete values.
  • No duplicate values: Sets cannot have two items with the same value.

Length of a Set:

We use the len() funtion to get the length of the set

print(len(set_example))

The set() Constructor:

The set constructor is used to create a set.

set_constructor=set(("Digital","Scholorship","Hub",2))
print(set_constructor)

Inserting elements into a set:

To insert a single element into an existing set, the add method is used.

set_example.add("Python")
print(set_example)

Instead of a single element, if you want to append another set or any other iterable object(tuples, lists, dictionaries), we can use the update method

list_a=["Beginners","Workshop"]
set_example.update(list_a)
print(set_example)

Deletion of sets:

To delete an element in a set, we use the remove and discard methods.

set_example.remove("Beginners")
set_example.discard("Workshop")

the key difference between these two methods is that remove raises an error if the item to remove does not exist, where as discard does not raise an error.

clear(): This methid will empty the set

set_example.clear()

del: this keyword is used to delete the set completely:

del set_example

Applications of sets

Key Points


String Manipulation

Overview

Time: 0 min
Objectives
  • To use python functions to manipulate strings

What is a String?

As mentioned earlier, a string is a collection of one or more characters put in a single quote, double-quote or triple quote.. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash.

Example: “Hub 2”,’Digital Scholorship hub’.

What is String manipulation?

String manipulation is the process of handling and modifying strings for various purposes like parsing etc.

Why is String Manipulation important?

Most of the data obtained from the user, is in the form of text. Applications use text boxed extensively to capture data accuratley. Therefore, dealing with strings is a top priority for programmers and data enthusiasts alike.

How to manipulate Strings?

Python offers an array of inbuilt functions aimed at helping with string manipulation. A few of them are discussed below.

Creation

We create a string by enclosing characters in either “ “ or ‘ ‘.

s= "Digital Scholorship Hub 2"
print(s)

We can also type caste a variable into string using str().

s1=str(12345)
print(s1)

Accessing

Strings are accessed in the same way as lists, i.e using []

a=s[2]
print(a)

Length of a string

We use len() to find the length of the string.

print(len(s))

Search and index:

The find function(find(“String”)) is used to find if the string contains the characters passed as the argument.

print(s.find("H"))

The index function is used to find the index of the word in the string

print(s.index("Hub"))

Count

The count(count(“”)) function returned the number if instances of the word present in the string.

print(s.count(" ")) # returns the number of spaces present in the string.

Slicing

Slicing is used to access character of a string between two indexes. We use a:b to get the set of characters. It returns the characters in the string from index a to index b-1.

print(s[3:6]) # returns characters from index 3 to index 5

NOTE:

s[a:b] # items start through end-1

s[a:] # items start through the rest of the list

s[:b] # items from the beginning through end-1

s[:] # a copy of the whole list

Replace

We can replaces characters in a string using the replace function(.replace(‘’,’’)).

s2= "Math and Science"


s.replace("Science","Statistics") # replace Science with Statistics.
print(s)

Upper case and Lower case

print(s.upper()) # print the string in upper case

print(s.lower()) # print the string in lower case

print(s.title()) # print the string in title case

print(s.swapcase()) # swaps the case of the string.

Reversal of a string

We can reverse a string using the reverse function(reversed()).

print(''.join(reversed(s)))

Stripping

In python, we have strip,lstrip,rstring function to remove characters from the beginning or the end of the string. if characters are not specified, it removes the white spaces.

strip(): removes the characters from both ends of the string.

lstrip(): removes characters from the left end of the string(leading characters)

rstrip(): removes characters fro the right end of the strings(trailing characters)

s=" this is an example "
print(s.strip())

Concatenation

Concatenation means to attach two strings. We can use the ‘+’ operator to concat strings.

s3="Hello"
S4="World"

print(s3+s4)

Join

The join function is used to convert a list into s string.

print(" ".join()) # adds a space between each character.

Testing.

We can test the string for numbers, alphabets etc using the following functions

s = "Digital Scholorship Hub"

s.isalnum() #checks if all the characters are alphanumeric 

s.isalpha() #checks if all the characters are alphabetic

s.isdigit() #checks if string contains digits

s.istitle() #checks if string contains title words

s.isupper() #checks if string contains upper case

s.islower() #checks if string contains lower case

s.isspace() #checks if string contains spaces

s.endswith('d') #checks if string endswith a d

s.startswith('H') #checks if string startswith H

Key Points