0% found this document useful (0 votes)
8 views12 pages

Week 4 Slides

The document covers Week 4 of a programming course focused on Python for data science, including topics such as control flow (if, else, loops), functions, and sorting algorithms. It provides examples of using break and continue statements, defining functions with input arguments and default values, and highlights the difference between return and print. Additionally, it includes practice exercises for implementing functions and sorting algorithms.

Uploaded by

anniezw120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Week 4 Slides

The document covers Week 4 of a programming course focused on Python for data science, including topics such as control flow (if, else, loops), functions, and sorting algorithms. It provides examples of using break and continue statements, defining functions with input arguments and default values, and highlights the difference between return and print. Additionally, it includes practice exercises for implementing functions and sorting algorithms.

Uploaded by

anniezw120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

INF1340H F

Programming for Data Science


Week 4

Amirsina Eskandarifar
Review
 With what we have learnt so far, we can write any program and
implement any logic or algorithm!

if, else, elif for


Python
Boolean while
operations
type range
Data types
Quiz

 questions
Break and Continue

 What should we do to quit and leave the loop


in the middle?

 For example:
 Write a program to search for a character in the
given string, if it has the character print true.
Otherwise, false!
Break and Continue

 What should we do to skip rest of the codes in


the loop and go to next iteration?

 For example:
 Write a program to take both a sentence and a
character. Then, print words with that character?
Functions

𝑎,𝑏

𝑦
Functions

 Functions are blocks of code that you may want to reuse


repeatedly in your program.

 Python has some built-in functions, like: max, min, sum, avg, string
functions, type, print, list, int, …

 Full list of built-in functions in Python:


 https://docs.python.org/3/library/functions.html

 Each one of these is just a block of code (including some basic logic)
for programmers to make their life easier!
Functions in Python
Input
arguments
def name_of_function(a , b , c):

Body of function &


print("Hello from a function") calculations

return 0 Output and return value


(optional)

Var_1 = name_of_function(a , b , c) Calling and executing

 Be careful about colon and indentation


Functions:
Input Arguments and Default values
 You can set a default value for your input arguments
 Example: print command: print(‘hello’, end = ‘ ‘)
 How to define a function with default argument?

def power_func(a = 1):

return a**2

print(power_func())
Functions:
Difference between return and print?!
 Write a function to take two numbers and return the summation of
them?

def add_func(a , b): def add_func(a , b):

return a + b print(a + b)

add_func(1,2) add_func(1,2)

Var_1 = add_func(1,2) Var_1 = add_func(1,2)


print(Var_1) print(Var_1)
Functions: Practice
 Write a function to take two lists of numbers, and if they have the
same length return a new list of multiplication of those two lists, if
they are different in length don’t return anything and print: ‘error!’

 List_1 = [1,2,3]
 List_2 = [4,5,6]
 Then return: [4,10,18]

 List_1 = [1,2,3]
 List_2 = [4,5,6,7]
 Then don’t return anything and print: error!
Sort Algorithms:
Bubble, Selection, and Insertion
Sorts
There is an array of number, and we want to sort them

1 9 2 8 5 4 3

 https://www.youtube.com/watch?v=JU767SDMDvA
 https://www.youtube.com/watch?v=g-PGLbMth_g
 https://www.youtube.com/watch?v=xli_FI7CuzA

You might also like