Eric Brauer
As discussed, we are introducing the Fundamentals of Programming, including:
These patterns are common to most (all?) programming languages.
How many of you prepare coffee/tea in the morning? Break down your process into steps. Now break it down into more steps. When we communicate to humans, we can skip some information because it is implied. We can’t do this in programming.
Example of Assembly code, which is a low-level language (very close to the hardware). Even this has to be compiled into machine code…
There are two ways to use Python:
Generally, the interpreter is good for trying things out, but we will be focusing on creating scripts.
This code will print the string to your terminal.
Notice the quotes around the words, this is important! You can also use single quotes (’’).
input
is a way for us to prompt the user for
information. This is a function.
x = 3 # integer: whole number
word = "Hello" # string: a series of characters
square_root_of_2 = 1.4142 # float: a number with a fractional component
We often need to store pieces of data. We use variables to do this. On the left, the variable name. On the right, the value assigned to it. = separates the two.
Here we have an integer, a string, and a float.
Functions are named blocks of code. Python has many, many builtin functions. Functions often have input (arguments) and output (return values). Let’s look at an example:
In this example, the function named input
takes a string
as an argument (inside the parentheses) and is returning another string,
which we can store in a variable called x.
Example: lab1a.py
#!/usr/bin/env python3
Comments are very important, but often overlooked by students!
Explains the code to other programmers, and to yourself (after time has passed).
#
'''
Writing comments is an art. But generally, don’t explain what your code does (it’s usually obvious). Explain why your code is doing what it does.
The Python Interpreter also has some built-in functions that you may find useful.
help()
type()
The good (and bad!) thing about Python is that datatypes are implicit.
That means they aren’t obvious (but will still cause you issues!)