Tutorial 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

PCL 1 - Tutorial 7

Luc, Isabelle, Lucas, Maritina


WELCOME
Overview
1. Midterm Review Thanks for the
last 7 weeks! I'll
see you in ECL
2. Lecture Material Review:
1. Types and classes

3. Debugging Tips

4. Quizizz!
Midterm Review Guidelines If you don't
know, now
you know,
folks!
• Please put everything except
your smartphone/laptop in your bags
• No writing utensils on the desk
• During review, the exam paper
must always be visible to the front of
the classroom
• Attempts at dishonesty will be handled
according to the PhF Honor Code
Midterm Handout, based on last names

• A – E: Luc
• G – P: Maritina
• Q – S: Lucas
• T – Z: Isabelle

Please do not open your exam until told to do so!


Midterm Review

• Please recount the points – there is always a possibility we


made a mistake

• If you are unsure why you received a certain score, please


raise your hand

• After the review ends, we cannot accept make further


changes to your score outside of exceptional circumstances
You may now open your exams!
Re module
import re

txt = "The rain in Spain"


x = re.search("Portugal", txt) / x = re.findall("ai", txt) Just a quick recap
print (x)
findall(not so trivial) : searches for all matches of See also Simon's slide nr46/58
of l08-objects-and-strings-slides.pdf
a pattern in a string and returns them in a list.
for his example

GROUP BEHAVIOUR
No Groups One Group Multiple Groups Non Capturing Groups
findall() returns a list findall() returns a list of findall() returns a list of Use (?:...) for parts of the
of the entire matches. matches from that group. tuples, each containing the pattern you don't want to
matches of the groups. capture. They don’t affect
findall()'s output.
re.findall(r'(\d+)', '123 abc')
re.findall(r'\d+', '123 abc') gives ['123']. re.findall(r'(\d+)(\w+)', '123abc') re.findall(r'(?:\d+)(\w+)', '123abc')
gives ['123']. gives [('123', 'abc')] gives ['abc']

Raw Strings: Use raw strings (r'') for patterns to avoid issues with Python’s escape sequences.
Functions and Control Flow

A function's code isn't


executed until the function Functions affect control
A function in Python is
is called by its name, flow by allowing the
defined using the "def"
followed by parentheses. program to jump to the
keyword. The function
This can happen function's code when
body contains the code
anywhere in the code called and return back
that executes when the
after the function is once the function finishes
function is called.
defined. executing.

Tutors' nr1 Best Seller resource for


understanding Control Flow and debugging:
https://pythontutor.com/
Debugging Simple Debugging Steps
• Use Print Statements: Put print() in your
function to show what's happening. This
Common Problems in Functions helps you see the values of variables at
• Wrong Inputs: Sometimes, you might give the different points.
wrong kind of data to a function. • Use PythonTutor: This helps you to
• Unexpected Results: Your function might not give visualize what's going on in your code and
back the answer you expect. how you might be able to fix it

• Mixing Up Variables: Getting confused between • Go Step by Step: In your IDE use the
different types of variables (like those used only debugger to go through your code line by
inside a function and those used throughout your line. It helps you see exactly what's
program). happening at each step.
• Check for Mistakes: Make sure your
function is getting the right inputs and
check each part of your function to see
AND READ YOUR ERRORS ! where it might be going wrong.
Errors, a crash course A lot of the time, your errors will tell you exactly what's wrong and where!
Python's pretty cool like that! Here's more: https://docs.python.org/3/tutorial/errors.html

What's wrong here?


Types and Classes
Types / Classes
In most cases, the terms "type" and "class" mean the same thing

• Classes are blueprints for objects Blueprint:


• The type of an object is the blueprint it was created from
frog

Pepe- Kermit-
type frog type frog
Objects - Kermit and Pepe Themselves

Kermit and Pepe are 'objects'


created from the Frog class. They
have their unique styles and
personalities but share the common
features from their frog blueprint.
Shared Class, Unique Objects: Attributes

As objects of the "Frog" class, Kermit and Pepe


would share common attributes such as:

✓ Having four legs


✓ Being green in colour
✓ Fashion icons
✓ Living in or near water environments

These attributes are inherent to their "Frog"


class, but individual properties like Kermit's
musical talent with a banjo and Pepe's
internet meme notoriety distinguish them as
unique objects.
More facts about Objects: Methods
Methods are like actions or behaviours that objects of a class can perform.

Pepe's method: makeMeme()


This method allows Pepe to create a new
meme. By calling pepe.makeMeme(), Pepe Kermit's method: playBanjo()
generates his latest internet sensation. This method represents Kermit's unique
ability to play the banjo. When we call
kermit.playBanjo(), we can imagine
Kermit playing a tune.

• Methods are functions that define the behaviour of objects in


programming.
• Objects from a class share common methods (e.g., jump(), croak()).
• Individual objects may also have unique methods that showcase
their specific traits.
Strings & Unicode
Strings A string in Python is a sequence of characters. It can be anything from a
single character to an entire novel.

Docstrings
Documentation Strings
• special strings that describe what a function, class,
module, or package does.
F-strings • important part of writing clean, maintainable code.
Formatted strings literals ⇢ """This is a docstring. It explains the purpose of the
• offer a concise and convenient way to embed function."""
expressions inside string literals, using curly braces {}
• efficient and readable
⇢ name = "Kermit"; greeting = f"Hello, {name}!

You could also


use .format()!
Strings: methods … There are many more!

Method Description
capitalize() Converts the first character to uppercase
Converts string to lower case for caseless
casefold()
matching
count(pattern, string) Counts occurrences of a substring
find(pattern, string) Finds the first occurrence of the substring
Joins the elements of an iterable into a
join(iterable)
string
Replaces occurrences of a substring with
replace(pattern, new pattern)
new

split(sep=None, maxsplit=-1) Splits string at sep and returns a list

format() Formats string into nicer output


Text Encoding

Understanding text encodings is crucial for:


• Correct text display across sources
• Prevent data corruption
• Optimize storage space
• Encodings define how byte sequences correspond to
• Support global languages characters.
• Meet legal standards • The size of byte sequences increases from ASCII to
UTF-16.
(ASCII < LATIN-1 < UTF-8 < UTF-16)
• UTF-8 is commonly used, but be cautious of Windows-
1252 from certain Windows applications.
Before we move on, we'd love your input to help us improve!

QUICK SURVEY
IT'S QUIZZIZZ TIME!
Questions?

Too cool for school? --> click here


@uzh_cl
@clatzh

You might also like