0% found this document useful (0 votes)
18 views

01 Processing Output

This document provides an overview of input, processing, and output in computer programs and introduces Python programming. It discusses setting up the Python environment, installing Python, and running Python code on Windows and Mac operating systems. It also covers basic Python commands, writing and running a first Python program, using comments, variables, data types, and math operators. Finally, it introduces defining functions in Python.

Uploaded by

Anubis Mage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

01 Processing Output

This document provides an overview of input, processing, and output in computer programs and introduces Python programming. It discusses setting up the Python environment, installing Python, and running Python code on Windows and Mac operating systems. It also covers basic Python commands, writing and running a first Python program, using comments, variables, data types, and math operators. Finally, it introduces defining functions in Python.

Uploaded by

Anubis Mage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Unit 01:

Input, Processing, and Output


Anthony Estey
CSC 110: Fundamentals of Programming: I
University of Victoria
Just in cased you missed it in the intro slides…

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

u A typical computer program performs a three-step process


u obtain input: any data the program receives while it is running
u perform some processing on the input: this could be simple (e.g., arithmetic
calculation) or complex (e.g., something we would classify as ‘artificial
intelligence’)
u produce output: based on the input and processing
u The three-step process can also be made arbitrarily complex
u For example, after obtaining input and performing processing...
u ... some other input may be needed.
u We will work backwards, looking at output first

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:

u In an editor, type the following:

u Save the file with .py as extension, for example myprogram.py


u In the command-line, go to the directory the file is saved, and then
execute the program by typing python followed by the file name:

11
What does this tell us about statement execution order?

u Statements are executed in the order they appear


u From top to bottom
u This is often referred to as running in sequential order
u In the next two units we will see how to:
u control which sequences of statements that are executed
u repeat a sequence of statements as many times as needed
u … but for now let’s focus on some basic python statements

12
Comments

u Notes of explanation within a program


u Ignored by the Python interpreter at run-time
u Purpose is to communicate information to another human

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?

u Sometimes when we run our code we get syntax errors


u These will be difficult to interpret at first, but will start making more
sense as we practice with Python

14
Variables

u A variable is a name that represents a value stored in memory


u Why?
u It’s much easier to refer to a name than an actual computer memory address
u We assign a value to a variable with the = operator
u The result of the expression on the right hand side of the = is stored to variable
named on the left side of the =
u Examples:
u num = 5 # stores 5 to the variable named num
u name = "Anthony” # stores "Anthony" to variable name
u num = num + 3 # adds num (5) + 3 to get 8
# then stores 8 to the variable num

15
Variable naming rules

u Rules for naming variables in Python:


u First character must be a letter or an underscore (_)
u After the first character, letters, digits, or underscores can be used
u Variable names are case-sensitive
u Variable names cannot be a Python keyword

u General convention: a variable name should reflect its use


u We will use typical Python naming conventions:
u Begin with a lower-case letter
u Multi-word variable names will have words separated with _
u Ex: last_name = "estey"

16
Python keywords

u Taken from
https://docs.python.org/3/reference/lexical_analysis.html

17
Types

Example Values Type Description


"Anthony" string type: a sequence of 0 or more
"csc110” str characters enclosed in quotations
"some words"
5, -9, 11523 int integer type: a negative or positive
whole number
4.2, 3.14, -8.2223 float float type: a negative or positive
floating point number (includes
decimals)

18
Python math operators

Symbol Operation Description


+ addition Adds two numbers
- subtraction Subtracts one number from another
* multiplication Multiples one number by another
/ floating point Divides one number by another
division - the result is a floating point number
// integer Divides one number by another
division - the result is an integer
- the decimal portion is truncated
% remainder Divides one number by another and gives the
remainder of the division
- the result (remainder) is an integer
** exponent Raises one number to the power of another
19
Operator precedence

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?

u General form: u Concrete example:

# function description # prints Anthony


def fn_name(): def print_name():
tab statement1 name = "Anthony"
tab statement2 print(name)
tab …

22
Function naming rules def print_name():

u Rules: def calculate_area():


u Cannot use key words as a function name
u Cannot contain spaces
u First character must be a letter or underscore
u All other characters must be a letter, number or underscore
u Uppercase and lowercase characters are distinct
u We will use typical Python function naming conventions:
u Begin with a lowercase letter
u Typically will contain a verb (action word)
u Multi-word variable names have words separated with ‘_’

23
Defining vs calling a function

u Defining a function allows us to name and group a set of statements


that perform a specific task…
u but the function is not executed unless the function is called

2 functions are defined


(print_greeting and main), but
are not executed until called

when the program begins, there


is a call to the main function,
which will execute main

inside the main function there is


a call to print_greeting()

24
This changes the flow of execution!

u Before all statements were executed from top to


bottom. Often specific statements only need to be
executed at specific times.
u Defined functions are only executed when called

25
Escape sequences

u How do we print Anthony said, "Welcome everyone!"


u Problem - quotations within our string!

Escape sequence Effect


\n output prints a new line
\t output prints a tab
\" output prints quotations
\\ output prints backslash

26
Putting it all together

u Write a program that produces the following output:

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

u A magic number is an unexplained numeric value that appears in a


program’s code. Example:

amount = balance * 0.069

u What is the value of 0.069? An interest rate? A fee percentage? Only


the person who wrote the code knows for sure!

29
The problem with magic numbers

u It can be difficult to determine the purpose of the number

u If the magic number is used in multiple places in the program, and


needs to be updated, it is very tedious to update it in every location

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

You might also like