# Intro to Python
## Week 3 Tues
## `def`ining better functions ##
and `for` loops
#### Virtual Office Hours 10AM & 7PM: #### [sdccd-edu.zoom.us/my/dlane](https://sdccd-edu.zoom.us/my/dlane) #### ``[slides.nlpia.org](https://slides.nlpia.org/)`` --- ## Attendance 1. walk up to the whiteboard 2. write your first name 3. write a keyword or function name 4. in python that's: ``f'{your_first_name}: {keyword_or_function}'``
## Hint: Use Sublime Text Hit `[Ctrl]+[P]` to find your `wk2.py` file
**OR**
Hit `[Ctrl]+[Shift]+[F]`, type part of a Python keyword, then hit `[Enter]` note: Be prepared to explain to the class why you like it. --- ## `str.`_[TAB]_ active learning --- ```python >>> text = 'hello world' >>> text ``` ``` 'hello world' ``` --- ```python >>> text = 'python rules' >>> text ``` --- ```python >>> text = 'python rules' >>> text 'python rules' ``` note: - that's why it is called a string literal - because the variable contains literally the same value as the python expression - the root of literal is the same as for literate and literature - this is the real meaning of "literally" - the slang meaning is "honestly" or "truly" --- ```python >>> str.title('hello world') ``` `'Hello World'` ```python >>> print('hello world'.title()) ``` `Hello World` --- ```python >>> str(1e6) ``` ```python '1000000.0' ``` note: - why single quotes? - what happens if you ` --- ```python >>> text = 'Hello World!' >>> is_enthusiastic = '!' in text >>> is_enthusiastic ``` ``` True ``` --- ```python 'hello' == 'Hello' ``` ``` False ``` ```python >>> 'ello' in 'Hello' ``` ``` True ``` note: - Hopefully you were able to predict these results --- ## How can you learn about `str.replace()` ? ```python help(str.replace) ``` ``` ... replace(self, old, new, count=-1, /) unbound builtins.str method Return a copy with all occurrences of substring old replaced by new. count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. ... ``` --- ## function signature for `str.replace()` ? ```python str.replace(self, old, new, count=-1, /) ``` --- ## How many arguments does `str.replace()` have? ```python >>> len(self, old, new, count) ``` ``` TypeError: len() takes exactly one argument (4 given) ``` --- ## How many
MANDATORY
arguments does `str.replace()` have? ```python >>> len(self, old, new) ``` --- ## How many
MANDATORY
arguments does `"Hello World!".replace()` have? ```python >>> "Hello World!".replace(old='Hello', new='Bye') 'Bye World!' ``` ```python >>> str.replace("Hull", 'l', 'k') >>> 'Hull'.replace('l', 'k') ``` ``` 'Hukk' ``` --- ## `str.replace` skills ```python >>> "Hull".replace('ll', 'lk') ``` ``` 'Hulk' ``` --- ```python str.split() str.join() ``` note: - Hopefully you've already seen these in your playful `str.[TAB][TAB]` exploring --- ## Announcements ### [syllabus](https://sdccd.instructure.com/courses/2505751/assignments/syllabus) - Attendance drops Sunday and Monday - Labor Day on Monday! - Give me feedback! ### Zoom Office Hours Monday 10AM & 7PM ### [sdccd-edu.zoom.us/my/dlane](https://sdccd-edu.zoom.us/my/dlane) note: - good news and bad news - I start dropping absentee students on Sunday - Labor Day on Monday! - I will be at zoom office hours on Monday - Show up at office hours if you have not attended most of our class sessions - Making attendance is not enough, you need to participate for the entire class --- ## Assignments due Sunday - [Runestone.Academy Module 2](https://runestone.academy/assignment/student/chooseAssignment) - [Canvas Module 2](https://sdccd.instructure.com/courses/2505751/modules) note: - next week [GitLab.com/mesa_python/fall26](https://gitlab.com/mesa_python/fall26) --- ## ``def``ine `print_and_return()` ```python def print2(text): return print(text) ``` note: - improve python style - what does `print()` return - what does our version of `print` return - add a docstring --- ## Hints - improve python style - what does `print()` return? - what does our version of `print` return - add a docstring --- ```python def print_and_return(text): print(text) return text ``` --- ## Hints - add a ``__doc__``string (a special ``# comment``) first! - make it useful in a text adventure or conversation - add another argument --- ```python def greet(greeting, name): """ Greet the user by name enthusiastically! >>> greet('Hi', 'bob') 'Hi Bob!' """ text = greeting + ' ' + name + '!' print(text) return text ``` [starter code](./wk2/wk2.py) --- ```python for i in range(3): print(i) ``` [starter code](./wk2/wk2.py) note: - Which words are keywords? - Notice any new punctuation or whitespace? - Which words are built-in function names? - Which words are variable names? - Which words are literal values? --- Have a play with the new keywords and functions for this week: - `def` - `return` - `for` - `range` note: - Which of these are Python keywords? - Which are built-in Python functions? --- Advanced looping (__iteration__) keywords: - `continue` - `break` --- ### Links 1. [slides.nlpia.org](https://slides.nlpia.org) 2. [starter code](./wk2/wk2.py)