Python basic arithmetic operators

Please submit files to Blackboard (see red font below). ○ Please clearly name your file with your last name, as indicated below. The grader would download many files at once, and having the last name in filename is crucial.

Homework 2

● Note: ​Plagiarism​ is serious. ○ This work should be finished independently. ○ Getting help from googling or reading books are encouraged.

● Submission

files at once, and having the last name in filename is crucial.

Objective ● Python basic arithmetic operators

○ +, -, *, /, **, % ● Python string formatting ● Write python code in a ​text editor​, save the file (some editor like PyCharm auto saves), then ​run code on

command line or inside of text editor ● Learn that body of python code, except for strings, are mostly ​lower case letters​ and numbers ● Functions: Learn how a ​function​ looks like from given examples. Modify functions and observe results. This is

only to help students with basic understanding of functions. In later lectures we’ll cover more on python functions.

Problem 1 — Change Counter

Objectives ● print() function

○ print(‘some string’) or print(variable_name), note that we’ll need to add quotes when printing strings.

● input() function ○ self learning how to use “input” function. please google. ○ Example: a = input(‘this is the prompt part of the input: ‘)

this will prompt on command line, then the command line input will be assigned to variable “a” ○ Note: if we input an integer in prompt, the type of the variable will be string, not integer. That’s why we

need specific type converting, using ​int()​ below, can also use ​eval()

● Convert String to Integer

○ As provided in code below, int(‘123’) will convert a string ‘123’ to integer 123, so we can perform arithmetic calculation with the integers

● Execute Python Program

2. Finish the THREE missing blocks indicated in the code below using “….”, according to the directions. Use text

editor to edit the code. Save it to a file named:​ ​change_counter_.py​, example, if your last name is Smith, then your file would be named: change_counter_smith.py.

○ PyCharm can “rename” a file. Google on how to. ○ Submit your code to blackboard, the file should be named: change_counter_yourlastnamehere.py

3. Run the code:

○ If you run on terminal: type: python3 path/to/your/code/change_counter_xxx.py On some computers it’s “python” not “python3”

○ If you run on PyCharm, right click, press: run change_counter_xxx.py

○ You don’t have to submit the result for running your code, running code is to make sure the code is well written and the output is as intended. Example Code Execution Screenshot. You’re only required to submit the python file, not result. The screenshot is only to show us how to run code.

○ An example of how I ran my code on Mac Terminal:

Problem 2: Math Module & String Formatting

Objective ● Learn what is ‘math’ module, how to use a ​method​ in math ​module​ by ‘dot notation’

○ math.sqrt(9) # math is the ‘module’, sqrt is a ‘method’ inside of math module ○ math.floor(5.8)

● f-String formatting Example:

○ >>> due_date = “tomorrow” >>> print(f”The due date of this homework is {due_date}.”)

Description If you still don’t have Jupyter Notebook installed, please email the instructor or TA. For homework requiring Jupyter, feel free to do it in a python file and submit it.

1. Follow the steps in ​‘Math.ipynb’​ provided, try each step and run it. No need to submit anything from Math.ipynb, this is only for practice.

2. Using Math.ipynb as reference, create a new python file using text editor, call it math_practice_.py

a. There should be a function called ‘main’, for how to define a function, follow examples from Problem 1 above. The function should take no argument, in other words, it should look like “def main():” with empty parentheses.

b. In this function ‘main’, we’ll play with ​math.pi, math.sqrt, math,exp, math.log10, math.sin​. Write code that will do the following calculation, then print the result using f-strings.

i. Calculate 10 * pi ii. Calculate square root of 15129 iii. Calculate e**10 using math.exp

If you are not sure what “e” is, see this or just google. https://en.wikipedia.org/wiki/E_(mathematical_constant)

iv. Calculate log10 of 10000 (observe what data type it returns) If you do not understand what log10 means, you may have to google

v. Calculate sin of pi c. Give each task a good description and print it. For example, this may be one of the outputs:

‘The value of 10 times pi is: 31.41592653589793’ d. Run your code to make sure it works and actually prints. e. Submit the ‘math_practice_yourlastname.py’ to Blackboard.

https://en.wikipedia.org/wiki/E_(mathematical_constant)
Problem 3 — String Formatting, str.format() method

Objectives ● Learn about python’s formatting: str.format() ● Jupyter Notebook:

you may find it necessary to learn some basic navigation commands or tricks for Jupyter Notebook, to make things easy. For example, you may modify a line on top, but want to re-run the next 10 lines. Google “jupyter notebook hotkeys” to learn more.

Description Go through the description and examples on https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3 Try the examples before reaching the section “​Padding Variable Substitutions​”. Try all the examples on Jupyter Notebook, you can change the code a bit and play with it, it’s easy to modify/erase/delete. In the end, please save the Jupyter Notebook file, name it ​‘string_formatting_.ipynb’​, (btw, what does the suffix ‘ipynb’ stands for? Google it if you’re curious). In your submitted ipynb file, each example should still play with the same functionality, but ​please use your own examples​, by changing the given code. Using existing examples from the webpage will result in point reduction. Submit file string_formatting_lastname.ipynb to Blackboard

https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3

The post Python basic arithmetic operators appeared first on Versed Writers.