# Numeric Operations ## and Pair Programming --- CS 65 // 2021-02-11 ## Extra Credit - Can receive +0.25% to your final grade for participating in various Drake activities - Only approved events will be worth credit - Need only send me an email with a description of what happened at the event you attended ## Extra Credit Activities - [Study Abroad 101 Info Session](https://calendar.drake.edu/event/study_abroad_101_info_session?utm_source=newsletter&utm_medium=email&utm_content=DETAILS&utm_campaign=Faculty-Feb-9#.YCLn7XdKhTY) + Friday, 2/12 from 10:30 to 11:00am - [Career Conference](https://calendar.drake.edu/event/be_unstoppable_flexible_fearless_career_conference?utm_source=newsletter&utm_medium=email&utm_content=DETAILS&utm_campaign=Faculty-Feb-9#.YCLmlHdKhTY) + Friday, 2/19 from 9:00am to 4:00pm - [Songs for A New World](https://calendar.drake.edu/event/songs_for_a_new_world?utm_campaign=widget&utm_medium=widget&utm_source=Drake+University+Calendar) Livestream Performance + Thursday, 2/25 from 8:00 to 10:00am # Assignment 3 - Is now posted - It is a **group** assignment - Your partner today is your partner for the assignment # Numbers ## Numbers in Python - **Integers** (int) + Expressed as: `5`, `-125`, `17`, … (no decimal point) + Restricted to **whole numbers** + Can be arbitrarily large or small + The larger the number, the more memory it will use on your computer ## Numbers in Python - **Floats** + Expressed as: `5.0`, `-125.39`, `17.333`, … (always has decimal point) + Are **approximate** and only uses a fixed amount of memory (usually 64 "bits") ## Operations on Numbers - Operations on **floats** usually produce **floats**) - Operations on **ints** usually produce **ints**)
- Evaluating `5 * 1.5` requires the numbers to be the same type + `5` can be easily turned into `5.0`, so... + Python will **always assume** you want it to convert the `int` into a `float` in this case # Functions ## Functions are Subroutines - Recall that a **subroutine** is a "named helper algorithm" that we can refer to and execute as many times as we like - If we want an algorithm to "make 10 sandwiches", we can write a subroutine to "make one sandwich" and execute it 10 times - This **saves a lot of work**! - In Python, subroutines are also called **functions** + The term is borrowed from mathematics ## Built-In Functions - `type(value)` + Returns the **type** of a value - `int(value)` + Creates a value of type `int` from the given value + Causes error if `value` does not encode a number - `float(value)` + Creates `float` from the given value - `str(value)` + Creates a value of type `str` from the given value ## Built-In Functions - `abs(num)` + "Absolute value" turns `num` into its positive form - `round(num)` + Rounds `num` to the nearest whole number - `min(n1, n2)` + Returns the minimum of `n1` and `n2` - `max(n1, n2)` + Returns the maximum of `n1` and `n2` ## Using Libraries - Only the most commonly used functions are available automatically. Other functions may be **imported** from a **library** (also called a **module**). - One useful library is `math` which provides many common math functions: + `ceil(num)` and `floor(num)` + `sqrt(num)` + Constants `e` and `pi` ## Using Libraries - We can use `math` to compute the area of a circle: ```py # A program to compute the area of a circle # # Author: # Titus Klinge import math r = float(input("What is the radius of your circle? ")) area = math.pi * (r ** 2) print("The area of your circle is", area) ``` - Must always have the prefix `math.` since the values are scoped within the module object # Self Checks # Pair Programming ## Highlights from Wray - **Discuss** your takeaways from the Wray article --- - What was Wray writing about? - Do you agree? - Why or why not? ## Highlights from Wray - On your own can be more efficient + Or can be *more* efficient since you can keep pushing each other forward in real time - Two heads can be better than one - Find a balance - Working in pairs you can notice more details + And build on each other's skills - Helps keep your code "clean" and neat so that it is easy to read ## Highlights from Wray - "Peer pressure" can be a motivation - "Expert programmer" (rubber duck talking) # Feedback from Students - After a few weeks, I had finally developed confidence in my abilities. One day in class, my partner pulled the keyboard out of my hand. It took another month to regain my confidence. - I found it very helpful to learn how to work with another person. - I sometimes take longer than others to come up with an answer. I felt like I was always holding my partner back. - My partner and I were working well together. Then they asked me out. - I loved pair programming. Working with another student allowed both of us to learn from each other about topics that we struggled with. - I work better on my own. Pair programming is a waste of my time. - I heard someone call their partner the B word. I hope I never get them as a partner. - Some partners were great. But some didn't listen and were hard to work with. - Pair programming helped me learn to work with others. As a senior, I still work with people I paired with in CS1. - I'd come up with a solution that seemed to work. Then my partner would erase it and type their own solution. - I didn't know anyone in the class when I started CS1; working in pairs let me meet a wide variety of people. - I'd come up with a solution that seemed to work. Then my partner would erase it and type their own solution. - One homework partner never seemed to be able to find a time to meet. - I didn't like working in pairs. There are times that I still don't. But I know that it's a skill I have to build. - I'm good at CS and could solve every problem. But I liked working in pairs because my partners regularly showed me other ways to approach the problems. - I was hoping to make friends with my partners, and I was a little let down when I didn't really actually make friends with anyone. - There were so many times that it was clear that neither of us could solve the problem on our own. We solved it together. - When I struggled, it was good to have someone else to work with me through the difficulties. - It's great to have someone to celebrate with when you finally figure out the solution. - Pair programming was fine, there was rarely any issues. However, at some point in the semester I think it would be appropriate to let us choose our own partners. # Lab Time