# 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
#### Open Sublime Text In Sublime Text: - Hit `[Ctrl]+[P]` to find your `*buggy*.py` file - Hit `[Ctrl]+[Shift]+[F]`, type a Python keyword, then hit `[Enter]`
### Write your first name ### and favorite python function ### on white board Be prepared to explain to the class why you like it. --- ## `str.` review ### `>>> str(1e6)` ```python >>> text = 'hello world' >>> text ``` ```python >>> 'hello world' ``` 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 'hello' in 'Hello' str.join() ``` note: - Hopefully you've already seen these in your playful `str.[TAB][TAB]` exploring --- ## New `str` skills ```python str.replace() 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)