Computating Module 0 Part C
Introduction to Python 3.7
This portion of the lab will introduce you to Python programming language version 3.7. To begin this portion of the assignment, copy over a directory with starting scripts for this lab. You can do this with the following command. Note that you should only have to do this once. cp -r /home/fri/lab_files_active/python3_intro/ ~
You can go to this directory with command
cd ~/python3_intro
.
Here you will find some introductory scripts: arithmetic.py booleans.py built_in_functions.py conditional_execution.py function.py helloWorld.py variable.py The python script "your_script.py" is the file you should be editing to practice. In other words, your_script.py is your assignment.
There are many resources online that can help you learn python outside of what you will be doing in this lab. This lab will reference Think Python, a book you can download for free . Another great reference is Python's documentation. If you are not satisfied with these recommondations simply Google a Python 3.7 tutorial for extra resources! Be aware as you google that there are different versions of this language with different syntax.
The following sections will take you through some introductory concepts to programming with the python programming language. Each section will include an example script, a few pages from the Think Python text book, and a few exercises to apply what you learned. Feel free to experiment with python by adding additional code to the introductory scripts; it's the best way to learn how to code!
Part C1: The First Program: print statements and comments
Part C2: Variables
Part C3: Arithmetic Operators
Part C4: Built-in Functions and Python Modules -- Math and Numpy
Part C5: Creating Your Own Functions
Part C6: Boolean Operators
Part C7: Conditional Execution
Part C1: The First Program: print statements and comments
Suggested Reading: Sections 1.5 and 2.9First, an introduction. We'll learn what a script is, how to comment, and how to print.
- Open the file helloWorld.py with your preferred text editor (Click here for options) and read through the comments.
- Make the changes as instructed in the script, save these changes, and exit out of the text editor.
- Run helloWorld.py . There are two ways to run a script: ./helloWorld.pyor call python explicitlypython helloWorld.py. The first method requires that you have execute permission to the script and that the script has the proper shebang for python 3. For our FRI cluster, the shebang for python 3 is#!/usr/bin/env python. Note that this is not a comment if it's the first line of a file!
- You should see that "hello world!" is printed on your screen like this:
Programming Exercises
- Open your python script your_script.py using your editor of choice.
- Adding comments to scripts you write is good practice. By doing this you will be able to understand the purpose of script you write and it will help you stay organized!
Comments start with a '#' followed by text. Write a comment which says "Part C1" to indicate that part of the assignment you are working on.
- (Assignment problem 1) Let's start with the simplest program; get your script to print "Part C1". Note that the quotation marks are not meant to be printed.
The print function is a python built-in function. This function is used in hello_world.py and you can copy that line over, change the content between quotation marks.
- (Assignment problem 2) Next, get your script to print "hello, my name is (enter name here)". Note that the perenthesis are not meant to be printed.
- (Assignment problem 3) Finally get your script to print a blank line. Be sure to run this script after editing to make sure you have correctly coded these new print statements. Your script output should be similar to this: