Professional Documents
Culture Documents
2
Setting up your environment
u You can write python code (for in-class activities and assignments), in
any editor or programming environment
u We recommend:
u Windows: Notepad++ (https://notepad-plus-plus.org/) (this is what you’ll use
in lab)
u Mac: either Atom (https://atom.io/) or Sublime
(https://www.sublimetext.com/)
Install Python
u You may already have Python installed but you want to ensure it is the
correct version
u See the following slides on how to check what version you have
u Download and install Python 3.7 - pick the download for your
Operating System (Windows, macOS, Linux) from:
https://www.anaconda.com
u Ensure you check off the box:
“Add Anaconda to my PATH environment variable”
Running Python on Mac OS
You will cover more on command line use in Lab1 but these basics will
get you started…
u Opening a Terminal:
https://youtu.be/gJMY3t4QJUo
u Running Python in interactive mode (and check your Python version):
https://youtu.be/j_T_5ePDeZI
u Running Python in script mode:
https://youtu.be/I7aCyggZ43E
Running Python on Windows
You will cover more on command line use in Lab1 but these basics will
get you started…
u Opening a command prompt window:
https://youtu.be/rh0l4GRh24k
u Running Python in interactive mode (and check your Python version):
https://youtu.be/8bwPMdD3h6o
u Running Python in script mode:
https://youtu.be/oTD-WNRLltU
Command-line
To open:
u Mac: ⌘+<space> opens Spotlight: then type Terminal
u Windows: <ctrl>+<esc> opens Start Menu: then type cmd
Command Description
ls (Mac) or dir (Windows) list the contents of the directory
cd <foldername> change directory into given folder
cd .. go back up to parent directory
pwd print working directory (the directory
you are in)
python <filename> run the given python file
Unit 01 Overview
u Reading:
u Textbook: 2.1 – 2.5, 2.7 – 2.9
u Learning Objectives: (You should be able to…)
u Display output with the print function using Python
u Add comments to your code
u Create, use, and modify variables
u Differentiate between the different variable types, and when to use each
u Perform arithmetic calculations on data
u Define and call simple function in python (without arguments)
u Use functions to control the flow of execution within a python program
8
Beginning to write programs
9
Displaying output with the print() function
u Terminology:
u Function: some pre-written code that performs an operation
u Pythons print() function: displays textual output onto the screen/console
u Argument: the data given to a function
u The argument we will give to the print function is the text we want displayed
u Example:
u print("Anthony") à outputs Anthony to the console
u print is the function being used, or called
u "Anthony" is the argument being passed to the print function
10
Writing your first python program:
11
What does this tell us about statement execution order?
12
Comments
u Comments begin with the # symbol, and all text on the remainder of
the given line is considered to be part of the comment
u Example:
# printing course textbook information
print("Starting out with Python")
print("Fourth Edition")
13
What about errors?
14
Variables
15
Variable naming rules
16
Python keywords
u Taken from
https://docs.python.org/3/reference/lexical_analysis.html
17
Types
18
Python math operators
Precedence Description
Highest Operations enclosed in brackets: ( )
Exponents: **
Multiplication/Division: *, /, //, %
Lowest Addition/subtraction: +, -
20
Effects of operators and types
Operator Applied to Results in
+, -, *, //, %, ** int, int int
/ int, int float
+, -, *, /, //, %, ** float, float float
float, int float
+, -, *, /, //, %, **
int, float float
+ str, str str
-, /, //, %, ** str, str ERROR
str, int str
*
int, str str
21
Defining a function
u We’ve seen how print() works, but how do we design our own?
22
Function naming rules def print_name():
23
Defining vs calling a function
24
This changes the flow of execution!
25
Escape sequences
26
Putting it all together
27
Exercise #2
u Write a program that has functions that print out the area and
perimeter of a circle. Assume the circle has a radius of 5.
u !"#$ = &" '
u (#")*#+#" = 2&"
28
Magic Numbers
29
The problem with magic numbers
u You take the risk of making a mistake each time you type in the magic
number in the program’s code, and maybe only one occurrence has a
very minor typo…
u Suppose you meant to type 0.069, but typed .0069 instead. This error can be
hard to see, but would cause mathematical errors in your program.
30