print()
input()
int()
, float()
, and str()
At the end of class on Wednesday, we wrote our first full python program using the atom
editor. You can use any editor you want to modify the file, but to simplify the course discussion, we will be using atom
throughout the course. Let's look at a few of the components of our first program and explore python syntax and semantics. A programming language's syntax describes the rules for valid placement of symbols and overall program structure. Semantics describes the meaning of the overall program as it relates to the syntax.
"""
My first python program
Joshua Brody
January 2019
"""
def main():
print("Welcome to cs21.")
main()
At the top of our program, we see a comment in triple block quotes
"""
text in here is ignored by python
but could be helpful to a human
reading the code
"""
Next we see the definition of our main function. For the first few weeks of the semester, our programs will always have this def main():
syntax. Later in the semester, around week 4, we will write other functions of our own besides main
. A function is a reusable piece of code that can potentially accept some input data and produce some output. For now, we simply place what we want our program to do inside the body of the main
function by indenting. So far, we only have a single print
(print()
is a built-in function), but we will soon add more.
The function definition is merely that: a definition. It does not actually run the code inside immediately. To see the output, we need to call the function by specifying the function's name (without the def
or the trailing :
) and including any required input in between the parentheses. main
does not require any input, so a simple main()
as our last line will suffice. Note that this line is not indented or inside the function defintion.
print()
statement inside the main function.main()
twice by repeating the main()
line twice at the end of the program.python3 welcome.py
cd
and ls
to navigate to your cs21/inclass/w01-intro
folderWe use computer programs to process data, but a program must have a way of storing information and referring to it later. Our primary way of doing this is through the use of variables. A variable is a named container that stores a value of a particular type. In the example below, place
is a variable that stores the name of a place. The data type currently stored in place
is a string containing a sequence of characters between quotes.
place = "Mitchell, South Dakota"
The syntax
<var> = <value>
assigns the variable name on the left hand side the value of the expression on the right hand side. It is illegal, or a syntax error to have a constant value on the left and a variable name on the right.
In addition to the string type (str) , we will initially focus on the integer (int) data type for whole numbers, e.g.,
numStudents = 35
year = 2019
diff = -6
and the floating point (float) data type for decimal data, e.g,
tempF = 34.1
gravity = 9.8
I encourage you to read Chapter 2 of the online text book for a longer discussion on topics regarding
Another good way to experiment is just to open a python shell and try a few one liners.
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 3+2
5
>>> "hello"-7
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'
>>> "hello"+"there"
'hellothere'
>>> 7.16-4
3.16
>>> x=4
>>> y=3
>>> x+y
7
>>> x=y
>>> y=7
>>> x
3
>>> y
7
>>>
Sometimes you may find it helpful or necessary to change a value of one type to another type. For example the value "7"
is a string because it enclosed in quotes. But the contents of the string, the character 7
is also a valid integer. You can convert a value to an integer using the int()
built in function. Try these examples in the python shell.
int("7")
int(3.2)
int("3.2")
int(5.7)
int("puppy")
ans="12"
int(ans)
Similarly, you can use float()
to convert a value to a float, or str()
to convert a value to a string.
One of the most helpful built in functions in the first few weeks of the course will be the input()
function. When provided a string prompt as input argument, this function prints the prompt to the screen and waits for a user to type something. When the user presses the Enter key, their response is returned as a value by the input()
function as a string. A sample usage might be
"""
A simple greeting program
Joshua Brody
January 2019
"""
def main():
name = input("What is your name?: ")
print(name)
main()
The program above is pretty terse. Can you modify it so it prints "Hello" followed by the entered name on the same line?
Suppose we are given the following problem: Have the user enter two integers. Then, print out the sum of these integers. Print out their product too.
Here are some steps a computer scientist might use to solve the problem:
atom
).The real innovation is in steps 1 and 2. Steps 3 and 4 are sometimes skipped, argued logically/mathematically, or handed off to new hires, grad students, or little brothers. Always do step 4 if you do step 3.
cd
cd cs21/inclass/w01-intro
atom ./
python3 math.py
Enter a number: 14
Enter another number: 10
14 + 10 = 24