CS111: Introduction to the Python Language

The following examples will familiarize you with the Python programming language.
The code is provided in the input cells (notice the labels In [ ]:).
To run the code in a cell, select it (by putting the cursor in the cell) and then click the Run button.
(it looks like the Play in a Music Player interface). Alternatively, press Shift+Return in your keyboard.
You'll see the result in the Out [ ]: cells. You can rerun the code in a cell at any time.
Feel free to change the code to experiment.

Table of Content

  1. Python as a Calculator
  2. Strings and Concatenation
  3. Variables
  4. Built-in Functions: max, min, type, len
  5. More built-in functions: Converting between types with str, int, and float
  6. Dealing with float values
  7. The useful print function
  8. Building interactive programs with input
  9. Error Types and Messages
  10. Test your knowledge
  11. Drills for Python Basics

1. Simple Expressions: Python as a Calculator

The Python interactive interpreter can perform calculations of different expressions just like a calculator.
Try to guess the result of each input, and then run the code to see the result.
The phrases precedeed by # are comments, they are ignored during the code execution.

Try out an expression of your own in the cell below. For example, an expression that has more than one operator, such as 2 * (3 + 4).

Summary

The results of an operator can depend on the types of the operand. For example: 7//3 returns 2 and 7.0//3 returns 2.0; neither returns 2.3333, but that is the result of 7/3. Make sure to understand what is the expected value type for a simple expression.

2. Strings and Concatenation

A string is a sequence of characters that we write between a pair of double quotes or a pair of single quotes. Run every cell to see the result.

The above was an example of string concatenation, chaining two or more strings in one.
How can you fix the issue of the missing space between 111 and rocks?
There are at least three different ways to do that. Try them out by modifying the input.
Alternatively, you can copy a cell (notice the Copy button -- 4th), paste it (5th button) and then make changes to the cell.

Solutions:

  1. Add space at the end of first string: "CS 111 " + 'rocks!'
  2. Add space at the start of the second string: "CS 111" + ' rocks!'
  3. Add space as a string on its own: "CS 111" + " " + 'rocks!'

Guess what will happen below:

Don't be scared when you see error messages like this. Instead, read the message carefully to understand what happened. This is a TypeError, which happens when an operator is given operand values with types (e.g. int, float, str) that are not allowed.

How can you fix it?

Repeated Concatenation: Guess the result!

Summary

The operators + and * are the only ones you can use with values of type string. Both these operators generate concatenated strings. Be careful when using the * operator. One of the operands needs to be an integer value. Why? See what happens when you multiply two string values.

3. Variables

A variable is a box containing a value that a programmer names or changes with an assignment statement, using =.
Variables can name any value.
Important: The symbol = is pronounced “gets” not “equals”!

Let us change the value stored in the variable named fav.

Will this change affect the variable aSum?
How would you check that?

What is the current value of fav? How would you check that?

An example of doing string concatenation with variables.

4. Built-in Functions: max, min, type, len

Finding the maximum or minimum of a series of two or more numbers with max and min.
The inputs to a function are called arguments, they are separated by commas.
Notice that a function has parentheses surrounding the arguments.

Finding the type of a value with the type function.

The function len that returns the number of characters in a string.

5. More built-in functions: Converting between types with str, int, and float.

The function str

The function int

How to fix the above error by using the int function?

The function float

6. Dealing with float values

The unexpected behavior of float values.

Fixing the behavior with round

The function round can be used with one or two arguments. We just saw that use with one argument.
Now let's check out the output when there are two arguments.
Try to guess what the second argument is doing.

7. The useful print function

This function will display characters on the screen.
Notice how we will not see the output fields labeled with Out[] when we use print.

When print is called with multiple arguments, it prints them all, separated by spaces.

The default space printed between multiple arguments to print can be changed by the keyword argument sep=string

When \n appears in a string, it represents a single character call the newline character. When printed, it causes the display to go to the next line.

Expression values vs. print

In the lines below, notice what happens when you execute the cell. Notice that sometimes you see an output cell, and sometimes you don't.

Question: why don't we see anything after executing the above cell?

Question: Can you notice the difference between the two lines above? Why do you think they are different?

It turns out that calling print returns the special None value. Python uses a None return value to indicate the function was called for its effect (the action it performs) rather than its value, so calling print acts like a statement rather than an expression.

To emphasize that calls to print act like statements rather than expressions, Thonny hides the None value returned by print and only outputs the printed expression. But there are situations in which the hidden None value can be exposed, like the following:

8. Building interactive programs with input

An alternative to "hard-coding" values in a program is to create an interactive program that asks the user for input. The built-in function input does exactly that.

9. Error Types and Messages

Try to guess what error type and message will appear in the examples below:

10. Test your knowledge

Use this section to try to answer the questions in the final slide of Lecture 2.

To create new cells, press the + button in the menu bar.

11. Drills for Python Basics

Compute 5 plus 6 and put it in the variable x

Compute 7 plus 15 and put it in the variable y

Compute 15 times 5 and put it in the variable z

Compute the remainder of dividing 15 by 5 and put it in the variable a

Compute the remainder of dividing 34 by 3 and put it in the variable h

Compute 6 divided by 12 and put it in the variable b

Concatenate the strings 'arbitrary' and 'string' and put it in the variable c

Create the string consisting of 1000 copies of the letter 'q' and put it in the variable d. Don't type 'q' 1000 times!

Use length and width to compute the area of a rectangle and store it in the area variable.

Use radius and math.pi to compute the area of a circle and store it in the area variable.

Convert the given string into an integer and store the number in the variable intified

Convert the given string into an floating point number and store it the variable floating

Make the string 'I am 1000 years old' using an expression with s1, s2, and age, and store the result in the string claim

Make the string 'I have 956 pizzas' using an expression with s1, s2, p1, and p2, and store the result in the string claim