2.1: FlowCharts and Python
- Page ID
- 204328
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)hypothes.is tag: s20iostpy01ualr
Download Assignment S2020Py01
Download Assignment LASTname_Fall2020_Py01
Learning Objectives
Students will be able to:
Content:
- Explain how to display data in Python using Thonny IDE
- Explain how to create a comment in Python
- Determine the difference between a string literal and a number
Process:
- Create print statements in Python3
- Create Python code that displays results to calculated addition and multiplication facts
Task 1: Update Anaconda
Your install of Anaconda is a fresh install, but there may have been updates to python libraries since the installation package was released. To update your anaconda package complete the following:
- In the start menu, type Anaconda to bring up the anaconda prompt. Click on the prompt to start anaconda.
This will bring up your anaconda prompt in a new command window.
2. Type conda update anaconda at the prompt and press enter (note: you may be prompted to type conda update -n base conda)
3. You will see that conda will determine what packages need to be updated. You should see a spinning cursor in the following window next to solving environment
4. There may or may not be packages that need to be updated. You can see in the screen below that there are many packages that need to be updated. Click y and enter to proceed:
5. You will see it updating as shown below. Specifically, you can see that the last line indicates that python needed to be updated. It will not download, extract and install updates. As it installs, the percent by each line will go from 0 to 100%
6. When it is complete, it will let you know:
Task 2- Watch video on Thonny:
Additional Thonny tutorials:
Step Through Python Scripts With Thonny (video)
https://realpython.com/python-thonny/
Model 1: Printing in Python
Output, one of the of the four main operations of a computer, is performed using a print statement in Python.
Flowchart |
Python Program |
Output |
print("The atomic number") print("for carbon is") print("6") |
The atomic number |
In your Thonny python shell, type the first line of python code above and press enter.
Critical Thinking Questions:
- What does the first line of Python code in the Python program do?
- Circle the Python code’s equivalent representation in the flowchart.
- Using the Thonny python shell, enter and execute the following statements. What does the code produce when executed?
- Was there a problem with any of the sample code in question 3? If so, what caused the problem?
- What caused the different formatting of output for samples “a” and “c” in question 3/
- Using the Thonny python shell, enter and execute the following statements. What do the following Python statements output?
Exercise \(\PageIndex{1}\)
Predict the results of each print statement and then using either the Thonny python shell on your computer or the Binder access to the LibreText Jupyter Hub validate your answers (you can cut and paste each question into the IDE/binder, the first one is already loaded)
- print (2+5)
- print (2*5)
- print ('2+5')
- print ("Age:",7)
- print ('Age:',2+5, "years")
- Answer a
-
7
- Answer b
-
10
- Answer c
-
2+5
- Answer d
-
Age: 7
- Answer e
-
Age: 7 years
a. print(2+5) b. print(2*5) c. print("2+5") d. print("Age:",20) e. print("Age:",2*10,"years")
- Examine the output for each statement in question 6.
a. What is the difference in the output for the statements in “a” and “c” of question 6?
b. What caused the difference?
c. Which of the statements contains a string literal?
d. What does the comma (,) do in the print statement in part “d” of question 6? How does it affect the spacing of the output?
e. Can you include both string literals and calculations in the same print statement? Explain.
Model 2: Documentation in Python
Documenting a program is essential for you as well as for others who may need to enhance or update your program at some future time. Any documentation or commenting in Python should begin with a “#”. The interpreter will ignore anything that comes after the “#” on a given line. See the example below.
Python Program |
Output |
# Programmer: Monty Python # Date: 3/15/2018 # Description: This program demonstrates the use of print # statements print("The mass of 0.5 moles of water is") print(0.5*18.02,"grams") |
The mass of 0.5 moles of water is |
Task 3: Run the program in Run current script mode.
In your Thonny IDE, choose File/New file.
In your Thonny IDE, choose File/Save and title the save file as Program1, and be sure to save as type “Python files”.
Copy the python program above into the editor window of your Thonny IDE.
Run the program by clicking the green run button , or hitting F5 of your function keys.
The output of the file will be shown in the shell window.
Task 4: Run the program again in debug mode.
To do so either go to menu Run/Debug current script or by clicking ctrl+F5
Critical Thinking Questions
- Examine the previous code and its output. What do the first four lines of the program do? (What did the python interpreter do with those four lines in debug mode?)
- What would happen if you placed a “#” in front of the code: print(0.5*18.02,"grams") in the previous program?
(after you predict what happens, put a # in front of the print statement, and rerun the program.)
Model 3: Computer system basic functions
There are four basic functions of a computer system:
Input |
The act of transferring information into the computer system. This can be achieved by using an input device (such as a keyboard or mouse), or through a previously stored program file.
|
Output |
The display of information from within the system to a user.
|
Processing |
The manipulation and control of data.
|
Storage |
The ability to save data in temporary and long-term repositories. Temporary data is stored in RAM, and long-term storage takes place on hard drives. |
10. using what you have done today as a guide, provide examples of how you employed each of the four basic functions of a computer in this activity
Application Questions: Use the Thonny Python Editor and Shell windows to check your work
1. Create a Python program containing four statements to implement the flowchart in below. Write the statements next to the flowchart and test your work.
2. Create one line of Python code that produces the output expected from the flowchart in application question 1 except that it is outputted on only 1 line.
(e.g. your output will be The formula weight of cyclohexane is 84.16 g/mol.
3. Create a Python program containing two statements that prints the following output. Have the program calculate the answers to the two arithmetic problems.
HomeWork Assignment
At the top of this page you can download a Word document that you can do your assignment in and then upload to the Google Drive folder.
1) After reading the https://en.wikipedia.org/wiki/Flowchart, create a flowchart using https://www.draw.io/ that would describe heating a sample to constant mass. The following video is a nice overview of how to use it https://youtu.be/Z0D96ZikMkc) (5 points)
[Insert Image Here]
2) Create a Python program that prints out the following information and upload it as a python file with the following file name LastName_py01_Q2.py (the ".py" is the python file extension) (10 pts)
Line 1 prints out the name of an element and its atomic mass
Line 2 prints out the name of another element and its atomic mass
Line 3 prints out the value of the addition of the atomic masses of the two elements
Line 4 prints out the value of the product of the atomic masses of the two elements
Sample Printout for Hydrogen and Chlorine would look like:
Hydrogen: 1.008
Chlorine: 35.453
Sum: 36.461
Product: 35.734
'"
Your program must contain documentation lines that include your name, the date, a line that states “Py01 Homework question 2” and a description line or two that indicates what the program is supposed to do. Take a screen shot of the program and the output and attach below:
[Insert Screenshot Here] (note, adjust all images so that they do not span a page break).
Python uses multiple data types. Using hypothes.is, find two different sites that describe data types in Python and write below a description of the difference between integers, floating point numbers and string literals. Be sure to tag the hypothes.is annotations with the chapter's tag: S20IOSTpy01ualr (case sensitive)
Copyright Statement
This work is licensed under a Creative Commons Attribution-NonCommercial-Shar