Conditional Looping, Functions, File Manipulation
Conditional Looping, Functions, File Manipulation
ØConditional Looping
ØFunctions
ØFile Manipulation
ØHands On
1
10/5/23
CONDITIONAL
LOOPING
2
10/5/23
CONDITIONAL STATEMENTS
IF STATEMENT
We use operators
from the previous
chapter as
conditions in the
statement.
3
10/5/23
ELSE STATEMENT
4
10/5/23
10
5
10/5/23
11
NESTED CONDITIONALS
12
6
10/5/23
LOOPING STATEMENTS
13
FOR LOOPS
14
7
10/5/23
FOR LOOPS
15
FOR LOOPS
16
8
10/5/23
FOR LOOPS
17
FOR LOOPS
18
9
10/5/23
FOR LOOPS
19
FOR LOOPS
20
10
10/5/23
FOR LOOPS
21
WHILE LOOPS
22
11
10/5/23
WHILE LOOPS
23
WHILE LOOPS
24
12
10/5/23
25
BREAK STATEMENT
When we include
the line break in
our loop, the
looping will stop.
26
13
10/5/23
CONTINUE STATEMENT
When we include a
continue statement in our
loop, Python will stop the
current iteration and
continue with the next. In
other words, Python will
skip the current iteration.
27
FUNCTIONS
28
14
10/5/23
29
TYPES OF FUNCTIONS
30
15
10/5/23
DEFINING A FUNCTION
This is an example of a
user-defined function.
We create a function
called greet_user()
that simply prints out a
greeting when called.
31
We pass arguments
into a function by
using a parameter.
32
16
10/5/23
WHAT’S IN A FUNCTION?
This is an argument.
33
PASSING ARGUMENTS
34
17
10/5/23
PASSING ARGUMENTS
35
PASSING ARGUMENTS
36
18
10/5/23
PASSING ARGUMENTS
This way, you can reverse the order of the arguments, as long as you
specify which parameter each associates to.
As
Notice
you that
can see,
we’ve
we’ve
reversed
reversed
the order,
the order,
but
butbecause
becausewewe
specify
specify
which
whichargument
argument
associates to which parameter, we get the
correct output.
37
DEFAULT VALUES
38
19
10/5/23
39
RETURN VALUES
Returning a
simple value
40
20
10/5/23
RETURN VALUES
41
RETURN VALUES
42
21
10/5/23
We can use
functions on lists
to save time
43
44
22
10/5/23
45
46
23
10/5/23
47
48
24
10/5/23
Thanks to the
arbitrary argument
we created earlier, we
can pass as many
arguments we want in
our function.
49
50
25
10/5/23
51
FILE
MANIPULATION
52
26
10/5/23
1.Opening a File:
file = open ('cuba.txt','r') # Opens ‘cuba.txt' in read mode
2.Reading from a File: content = file.read() # Reads the entire content of the file
3.Writing to a File: file.write ("This is lisa.")
4.Closing a File: file.close()
5.Appending to a File:
6.Checking if a File Exists:
7.Renaming and Deleting Files:
8.Working with Binary Files:
Note: Understand how to work with files (text files, CSV files, binary files, etc.)
53
1.Opening a File:
2.Reading from a File:
3.Writing to a File:
4.Closing a File: with open('example.txt', 'a') as file:
file.write("This is a new line.")
5.Appending to a File:
import os
6.Checking if a File Exists: if os.path.exists('example.txt'):
7.Renaming and Deleting Files: print("The file exists.")
else:
print("The file does not exist.")
8.Working with Binary Files:
54
27
10/5/23
1.Opening a File:
2.Reading from a File:
3.Writing to a File:
4.Closing a File:
5.Appending to a File:
6.Checking if a File Exists: import os
os.rename('old_file.txt', 'new_file.txt') # Renames a file
7.Renaming and Deleting Files: os.remove('file_to_delete.txt') # Deletes a file
55
FILE MANIPULATION IN
PYTHON (SELF STUDY)
56
28
10/5/23
• Make sure the file is in the same working directory as your IDE.
57
58
29
10/5/23
The read() function often returns an extra line of (empty) string at the end of the
output. To prevent this from happening, you can add the .rstrip() method at the end
of contents in the print() function.
59
60
30
10/5/23
61
Another way to do
this is by using
.readlines()
method on the file
object.
62
31
10/5/23
63
64
32
10/5/23
65
66
33
10/5/23
WRITING TO A FILE
67
WRITING TO A FILE
68
34
10/5/23
ANALYZING TEXT
69
If the file is
unavailable in the
working directory, an
error message will be
printed out.
If the file is
available in the
working directory,
count the number
of words.
70
35
10/5/23
71
STORING DATA
72
36
10/5/23
HANDS ON
73
CONDITIONAL LOOPING
1. Imagine an alien was just shot down in a game. Create a variable called
alien_color and assign it a value of ‘green’, ‘yellow’, or ‘red’. Write an if
statement to test whether the alien’s color is green. If it is, print a message that
the player just earned 5 points. If it isn’t, print a statement that the player just
earned 10 points.
2. Think of at least three kinds of your favorite pizza. Store these pizza names in a
list, and then use a for loop to print the name of each pizza.
3. Make a list called sandwich_orders and fill it with the names of various
sandwiches. Then make an empty list called finished_sandwiches. Loop
through the list of sandwich orders and print a message for each order. As each
sandwich is made, move it to the list of finished sandwiches. After all the
sandwiches have been made, print a message listing each sandwich that was
made.
74
37
10/5/23
FUNCTIONS
75
FILE MANIPULATION
1. Open a blank file in your text editor and write a few lines
summarising what you’ve learned about Python so far. Start
each line with the phrase:
In Python you can…
Save the file as learning_python.txt in the same directory as
your exercises from this chapter. Write a program that reads the
file and prints what you wrote three times. Print the contents
once by reading in the entire file, once by looping over the file
object, and once by storing the lines in a list and then working
with them outside the with block.
76
38
10/5/23
2. Write a program that prompts the user for their name. When
they respond, write their name to a file called guest.txt.
3. Write a program that prompts for the user’s favorite number.
Use json.dump() to store this number in a file. Write a
separate program that reads in this value and prints the
message, “I know your favorite number! It’s ____.”
77
THANK YOU
78
39