Week 1: Introduction

Week 1: Advanced#

About advanced material. The advanced material introduces advanced topics and tasks related to this week’s content. Problems in the advanced section may be open-ended or may include code that is not explained in detail. You are not required to read this material, and none of the mandatory exercises or exam questions will rely on this material.

Advanced Topics#

Advanced 1.1: Running a Downloaded Script#

Instead of creating and running your own Python scripts, you will now download and run an existing script.

Download the zip-file week01.zip and extract it, using your preferred method, into the directory you made for this week. From IDLE, open the extracted file downloaded_python_script.py. The file contains some print statements that may look unusual.

Run the file. What was printed?

Advanced 1.2: Terminal#

A terminal is a window where you can type commands directly to your computer. At this point in the course, we use Terminal to run Python scripts, but it can be used for much more. You might need it to control your Python installation, or other settings on your computer.

Here are links to two tutorials we recommend (where terminal is called command line interface, CLI):

  • CLI at Django Girls tutorial - An international non-profit organization Django Girls introduces girls to programming. Their tutorials are excellent for all beginners. This tutorial has an excellent video! You can find a link to the video at the beginning of the tutorial.

  • CLI at W3Schools - Educational website W3Schools is excellent for learning coding and web development, run by a Norwegian company. In this course, we will often link to W3Schools as an additional resource. Their page on CLI contains a table with basic CLI commands. Try to navigate to a folder, and display its content. You might see some hidden files, which are not shown in the file explorer.

Advanced 1.3: Interactive Python from Terminal#

To practice different ways of running Python, also without IDLE, try running interactive Python from the terminal. If you have never used the terminal before, refer to the Python Support: Terminal for a short introduction.

You should start an interactive Python session in the terminal by typing python3 or python (depending on your installation). You can then type Python commands directly into the terminal. To exit the interactive session, type exit() .

In the interactive Python session:

  • Print some text. For example, print your name.

  • Compute some numbers. For example, compute the area of the square with side length 5.12. Do you need to use print to see the result?

  • Write some code with a syntax error. For example, print(Hello World) . What happens?

  • Use the up arrow key after you have executed some commands. What happens?

Advanced 1.4: Running Scripts from Terminal#

To practice different ways of running Python scripts, try executing one of your scripts from the terminal, without starting IDLE. This is also shown at Python Support: Terminal.

To execute a Python script, you need to enter a command consisting of two parts: a first part saying which Python interpreter to use, and a second part saying which script to run. The first part is python or python3, depending on your installation. The second part is the name of the script you want to run.

If you start the terminal in a different folder, the name of the script needs to be the full path to the script. This approach is shown in the Python Support video. Alternatively, you may open the terminal in the folder where the script is located. Once you are in the correct folder, it is enough to type the name of the script, without the full path. You can open the terminal in any folder using the right-click menu of your file browser.

You can also open the terminal in a different folder, navigate to the folder where you have a script, and then execute it. For this approach, follow the steps below.

  1. Decide which script you want to run and make sure you know its location on your computer. This can be one of the scripts you created from IDLE.

  2. Open the terminal on your computer.

  3. Navigate to the folder where your script is located. For this, you should use the command cd to change the directory.

  4. Run the script by entering command python my_script.py, but replace my_script.py with the name of your script. You might need to type python3 instead of python, depending on your installation.

  5. Observe the output of the script - where is it printed?

Advanced 1.5: More About the print() Function#

When given more than one argument, the print() function will separate them with a space, and add a new line at the end. You can change this behavior by using the optional arguments sep and end. Try it out by running the code below.

name = "Sasha"
print("Hello ", name, "!", sep="", end=" ")
print("How are you?")

Advanced 1.6: The input() Function#

The input() function is used to get input from the user. Try it out by running the code below.

name = input("Enter your name: ")
print("Hello,", name)

Check also whether you can use input() without giving it a string argument.

Advanced 1.7: Weather Report#

The function input() allows you to make small interactive programs. For example, look at our weather report program below. Try it out.

temperature = input("Enter temperature (in degrees Celsius): ")
condition = input("Enter weather condition (e.g. sunny, rainy, cloudy): ")
print("Weather Forecast: It's ", temperature, " degrees and ", condition, ". ", sep = "", end="") 
print("We predict it will stay so for some time. Have a great day!")

Advanced 1.8: Adding Some Randomness#

A dialog is more fun when it is not fully decided in advance. Try running the code below a few times. We will later explain what import random does. For now, just run the code and observe the output.

import random
number = random.randint(1, 100)
print('My favorite number is', number)

Try also this example. You will learn later why square brackets are used. For now, just run the code.

import random
place = input('Where do you live? Enter the name of the place: ')
attribute = random.choice(['nice', 'cool', 'cozy'])
print(place, 'is a', attribute, 'place!')