title: ‘Lecture 2 Key Concepts Pt. 2’ |
author: |
- Eric Brauer |
A program is a sequence of instructions. Instructions accomplish one of three things:
You’ve already worked with displaying user output in Lab1:
To combine two datatypes in one print()
, put a plus sign
between strings and the variable. You also need to convert other data
types to strings:
str()
int()
float()
User input is any data entered by the user running your script. This is in contrast to data you, the programmer, enter into the code or call from elsewhere.
This allows for user interaction. The program can respond to what they enter, and do different things as a result.
Basic in concept, but complex in that it introduces an unknown into your program. It’s up to you to deal with that input properly.
Keep this in mind as you go through this course.
input()
year = input("Enter your year of birth: ")
The data in this object can then be used by the script at any point.
print('You were born in ' + str(year) + '.')
A command line argument is data entered on the command line after the name of the script when it’s first run. You may know these as positional parameters in the Bash scripting world.
./awesome.py variable1 variable2
Command line arguments are almost always preferable to using
input()
because they are easier to automate.
Before working with command line arguments it’s necessary to import
the sys
module. Fortunately it’s contained in our Python
Standard library.
Python includes an extensive library of modules that are installed alongside Python itself. To work with command line arguments, we need to:
import sys
This sys
module contains a lot of useful functions for
interacting with the computer system at a base level. Always refer to official
documentation to learn more.
There are other, third-party modules that are not included in the
standard library. You will need to install these using a tool like
pip
.
We will not be using these in our course.
Assuming you have imported sys
, and run your Python
program with arguments:
./awesome.py variable1 variable2
This data is then passed into a built-in list called
sys.argv
.
Conditional statements allow you to program paths, or reactions to data.
Whether this be in reaction to user input, or data you request from a file, it changes the flow of the program.
At the very least, Conditional Statements need:
If statements allow the program to respond if a specific condition is True. Take the following:
if condition == True:
print("Action 1")
print("Action 1 continued")
print("everything here runs regardless of the outcome")
Anything that is indented will only be run if the condition is true. The end of the indent indicates the end of the if statement’s action.
year = 2025
yob = input("Enter your year of birth: ")
if year – int(yob) < 19: # Note! < only works with integers, so we need to convert
print("Sorry, you are too young.")
else:
print("Okay, you are old enough.")
print("Have a nice day!")
Actions after else
will run if the condition is
False. You are creating two possible outcomes, but the
code will only follow one path.
We can evaluate multiple conditions for the same value. These are called if/elif/else statements.
You are creating three or more possible outcomes, the branch that is taken is the first condition that returns true.
When using if/elif/else, remember that order matters. The first condition that is true will be executed, all other conditions will be skipped.
You might also choose to make your conditions more specific
by using the logical operators and
and or
.
These allow you to test more than condition on the same line.
and
or
not
A loop statement allows us to loop through a section of code a set number of times. In a while loop, the script will continue to loop through the block of code while a condition is still true.
In other words, a while loop is very similar to an if condition, except that the while loop will continue to run the action for as long as the condition is True. In this example, we run lines 2, 3, 4 how many times?
Remember to avoid Infinite Loops. If the indented code of your while loop doesn’t contain a way to change the condition, you will never escape the loop (at least until you press Ctrl+d to terminate your Python program).
One way to exit a loop is to use break
.
==
>
>=
!=
<
<=