0% found this document useful (0 votes)
16 views

Tutorial 3

The document provides an overview of an exercise on learning Python programming basics. It outlines 6 tasks for students to complete that involve using concepts like strings, conditionals, loops, and comments in Python code. It also includes recaps of regular expressions and Unix/grep commands.

Uploaded by

Richard Salnikov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Tutorial 3

The document provides an overview of an exercise on learning Python programming basics. It outlines 6 tasks for students to complete that involve using concepts like strings, conditionals, loops, and comments in Python code. It also includes recaps of regular expressions and Unix/grep commands.

Uploaded by

Richard Salnikov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PCL 1 - Tutorial 3

Luc, Isabelle, Lucas, Maritina


WELCOME
OVERVIEW

Your first steps in focus of the tutorial:


Python.. exciting!!!

● Overview of exercise 2
● Recap of concepts introduced
in the lecture
● Ultimate regex/unix
commands/grep recap
Tips and Remarks:
Exercise 2
Task 1: get to know the Python interpreter, no
hand in

● IMPORTANT: This is a non- Task 2: Use input() to capture values. Remember


graded exercise . We to convert strings to appropriate data types
recommend that you solve before calculations. Consider the implications of
it and hand it in by 25. integer division versus floating-point division.
October 2023 at 6 pm. If
Task 3: Utilize Python's len() to measure string
you submit it in time, we
lengths and str.upper() for capitalization.
will give you feedback.
Task 4: use input() and if-elif-else statements
● 4 small python programs
and 1 comment exercise Task 5: use ctrl + c if you get endless loop

Task 6: comments are very important but don’t


exaggerate. Remember, comments should explain the
'why' or 'how', not just restate the 'what'.
String Slicing and Indexing
Word = "leopard"

● Word[0] -> "l"


● Word[-1] -> "d"
● Word[1:] -> "eopard"
● Word[:6] -> "leopar"
● Word[::2] -> "load"
● Word[::-1] -> "drapoel"
● Word[::-2] -> "daol"
IF – ELSE STATEMENT
IF – ELIF – ELSE STATEMENT
WHILE LOOP
Executing programs WHAT DOES THIS MEAN?
#!/usr/bin/env python3
In the terminal (Powershell or - is a shebang (or hashbang) that specifies
CMD on Windows): the script should be run with Python 3.

● Navigate to the directory where


the .py file is located with cd #-*- coding: utf-8 -*-
(cd, pwd, ls) - sets the source code's encoding to UTF-8.

● $ python3 my_program.py (might - In Python 3, this is default and often


also be python my_program.py) unnecessary.

● Important: check the python - Let's the interpreter know what encoding
version you’re using! is being used
Text Editors for Python-Files
● Python programs are basically simple text files with
a .py extension.
We'll give you
a short
● Use the text editors introduced in the first introduction to
VSCode!
tutorial

● Work with what works for you

● Integrated Development Environment (IDE)


- Very useful tools, especially for larger projects, but
there is a learning curve -> Use text editors for this
exercise, if you are not used to an IDE.
It’s QUIZZIZZ time
Questions?

I've lost my voice --> click here


regex ultimate recap
escape special character
\ If you want to match the literal period . in a string, use '\.'
If you search with the regex: 'ca\.' it matches: ca. but not cab, ca!, etc.

matches any character except newline


. If you search with the regex: 'ca.' it matches: cab, ca!, ca3, etc. but not ca\n or ca

mark groups
This groups multiple characters together. Useful for applying quantifiers to multiple characters or capturing
(...) substrings.
If you search with the regex: '(ca)+', it matches: ca, caca, cacaca, etc.

backreference to groups
\1 To find repeated words:
If you search with the regex: '(\w+)\s+\1', it matches: 'hello hello', 'bye bye', etc.

match at beginning (if at beginning of regex), negation (if not, e.g. [^x])
If at the beginning of regex, it matches the start of a line. If used within [], it negates.
^ Example:
- Start of Line Regex: ^ca, it matches: ca in cat but not in space
- Negation Regex: [^x], it matches: any single character except x.

match at the end


$ If you search with the regex: cat$, it matches: cat in The pet is a cat but not in catalog.
regex ultimate recap
\b \B \d \D
Matches word boundaries. Matches where \b does not. Matches digits. Matches non-digits.
'\bcat\b' matches cat in '\Bcat\B' matches cat in "scattered" matches 3 in "abc3def". matches a in "abc3def".
"The cat sat", but not in but not in "The cat sat".
"catalog" or "scatter".
\s \S \w \W
Matches whitespace Matches non-whitespace. Matches word Matches non-word

regexone.com
characters. characters.
'\s' matches the space in matches c in "cat sat". matches a in "cat_9". matches ! in "cat!".
"cat sat".
{n}
+ * ?
Matches the preceding
Matches 1 or more of the Matches 0 or more of the preceding Matches 0 or 1 of the
character or group exactly
preceding element. element. preceding element.
n times.
'ca+t' matches cat, caat, 'ca*t' matches ct, cat, caat, etc. 'ca?t' matches ct and 'ca{2}t' matches: caat but
etc. cat, but not caat. not cat or caaat.
Additional Regex Practice

regexone.com
unix commands recap
wc Count lines, words, characters
Show current directory
pwd Useful to if you need to know which file path
you're in
ls –l See files in directory
more file.txt Look into the file

sort -u Sort a file and remove identical lines


Delete all duplicate lines and counts
uniq -c
occurences
tail –n x Show x lines from end
head –n x Show x lines from beginning
grep options

-P Pear compatible regular expression

-h Hide filenames

-c count

-o Show only parts that were matched

-i Case insensitive matches

-B x Show x lines before match

-A x Show x lines after match

-C x Show x lines before, x lines after match

You might also like