Docsity

Python Environment Setup and Basic Programming Concepts, Lecture notes of Law

Toggle sidebar
bg1
print()
python3 ./username-lab1a.py
print('hello world' )
NET2008A โ€“ DevOps โ€“ Fall 2022 - Lab 1
Python Environment Setup
PART 1 - Setting Up Python 3 Environment
Install PyCharm
Download PyCharm Community Edition for free and install it on your
computer. Use PyCharm for PART 2 to PART 4.
Install Anaconda and JupyterLab
Following the installation guide of Anaconda on their
website. Use JupyterLab for PART 5.
To launch JupyterLab, open Anaconda-Navigator and launch JupyterLab:
webpage will open in your browser showing the JupyterLab interface:
Once you have JupyterLab set up, we will be using it for future lab distributions.
PART 2 - Creating the "Hello World" Python Script
You will learn to create a simple python script in this section. This python script will just call the Python built-in function print() to send the text
"hello world" to the screen. The "hello world" is an old traditional first program students usually are taught to create, which is based on the first
programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and Brian
Kernighan. You will learn how to run the Python script in the Python3 shell as well as to learn how to run the Python script from the bash shell.
Perform the following steps:
1. Create a new Python file in your ~/net2008/lab1 directory. Call it username-lab1a.py. The first Python code we will write is going to call the
print() function. A function is code that has been defined somewhere. Functions can take arguments, use these arguments in some way, and
then usually, but not always, return a result. The first function we will use is the "print()" functions, it's sole purpose is to send data to the
screen.
2. Add the following line into your source code file:
And run it from the command-line:
You will notice that nothing is printed even though we called the "print()" function. This is because we didn't pass any arguments to it, lets try again.
3. Modify your call to print() to include an argument ('hello world'):
This time we should now see that the python function "print()" has sent something to the screen - the words 'hello world'. In Python a word or a bunch of
characters like
pf3
pf4
pf5

Partial preview of the text

Download Python Environment Setup and Basic Programming Concepts and more Lecture notes Law in PDF only on Docsity!

print() python3 ./username-lab1a.py print('hello world') NET2008A โ€“ DevOps โ€“ Fall 2022 - Lab 1

Python Environment Setup

PART 1 - Setting Up Python 3 Environment

Install PyCharm

Download PyCharm Community Edition for free and install it on your computer. Use PyCharm for PART 2 to PART 4.

Install Anaconda and JupyterLab

Following the installation guide of Anaconda on their website. Use JupyterLab for PART 5. To launch JupyterLab, open Anaconda-Navigator and launch JupyterLab: webpage will open in your browser showing the JupyterLab interface: Once you have JupyterLab set up, we will be using it for future lab distributions.

PART 2 - Creating the "Hello World" Python Script

You will learn to create a simple python script in this section. This python script will just call the Python built-in function print() to send the text "hello world" to the screen. The "hello world" is an old traditional first program students usually are taught to create, which is based on the first programming example from the first C programming text co-written by Dennis Ritchie, the creator of the C programming language and Brian Kernighan. You will learn how to run the Python script in the Python3 shell as well as to learn how to run the Python script from the bash shell. Perform the following steps:

  1. Create a new Python file in your ~/net2008/lab1 directory. Call it username-lab1a.py. The first Python code we will write is going to call the print() function. A function is code that has been defined somewhere. Functions can take arguments, use these arguments in some way, and then usually, but not always, return a result. The first function we will use is the "print()" functions, it's sole purpose is to send data to the screen.
  2. Add the following line into your source code file: And run it from the command-line: You will notice that nothing is printed even though we called the "print()" function. This is because we didn't pass any arguments to it, lets try again.
  3. Modify your call to print() to include an argument ('hello world'): This time we should now see that the python function "print()" has sent something to the screen - the words 'hello world'. In Python a word or a bunch of characters like

'hello world' is called a 'string'. In the above example, a string was passed as an argument to the print function. These words are important for understanding and talking about different aspects of code.

  1. Note that there are similarities between the Python print() function and the Bash echo command, but Python is more picky than bash (which is a good thing). Try to run print without the brackets or without the quotes to see what happens. One of the things that makes a good programmer is debugging skills. The first and most important debugging technique is reading and understanding error messages. Try to understand what the errors are saying even if you think you already know what the problem is and already have some idea about how to fix it.
  2. Write the following code into our Python file. Note the she-bang line at the top of the file to run this script in the python3 environment. You will need to add this she-bang line for all python scripts you create for this course.

num1 = 5 num2 = 10 print(num1) print(num2) sum = num1 + num print(sum) print('The sum is: ' + sum) print('The sum is: ' + str(sum)) cd ~/net2 0 08/lab1/ ./username-lab1c.py Isaac is 72 years old! print( 10 + 5 )# addition print( 10 - 5 )# subtraction print( 10 * 5 )# multiplication print( 10 / 5 )# division print( 10 ** 5 )# exponents

  1. Lets create two new objects, num1 and num2, to play with.
  2. You can print the values in those integer objects:
  3. Now we will make a new object called "sum", and try some math: This will add the values contained in the integer objects together and assign the result to the object named "sum". However you will note that there is no data show up on the screen. Let's inspect the contents of the new object named "sum": Does this value look right? Are you sure?
  4. Now lets try printing this sum out with a string: What happened? Did you receive an error? This may have been the first time you've seen this error, but it won't be the last. What we tried to do is combine a string with a number, and this won't work. In order concatenate a sting and an integer object, we will have to use another builtin function called "str()" to convert an integer object to a string first. The "str()" function will return a string of your number and provide it as a argument to "print()". This function will not change the value of your object, your object is still an integer object.
  5. Issue the following: What did you notice this time?
  6. To gain practice, complete your python script with the following features: The script should have a Shebang line. The script should have an object called name The script should have an object called age The value of the name object should be Isaac The object age should contain a integer The value of the age object should be 72 The script, when executed, should print out "Isaac is 72 years old!" Example run:

PART 4 - MATH OPERATORS

In the previous section, you performed a couple of simple mathematical operations. In this section, you will learn some additional mathematical operations.

Perform the following steps:

  1. Try some of the following to see what happens in Python: NOTE: You must be careful when combining more complex math operators together. Python uses PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) to resolve math.
  2. Go over the below examples and see if you understand each situation:

cd ~/net2 0 08/lab1/ ./username-lab1d.py 10 + 2 * 5 = 20 print( 10 + 5 * 2) print(( 10 + 5) * 2) print( 10 + 5 * 2 - 10 **

print( 15 / 3 * 4) print( 100 / (( 5 + 5) * 2 ))

multiplication happens before addition

parentheses happen before

multiplication

first exponents, then multiplication, then addition and subtraction from left-to-

right # division and multiplication happen from left-to-right

the inner most parentheses are first performing addition, then parentheses again with mult

  1. To gain practice, complete your script with the following content and details: The script should have a Shebang line. The object x should contain a integer with the value 10 The object y should contain a integer with the value 2 The object z should contain a integer with the value 5 The script, when executed, should print out "10 + 2 * 5 = 20" (the printout should change if the values in the objects change) Example run:

PART 5 - Jupyter Labs

Repeat PART 4 in JupyterLab. Run all lines in JupyterLab and add a brief introduction using Markdown blocks. Save the notebook file as username-lab1e.ipynb (The .ipynb extension is specific for Jupiter Notebook files)

LAB 1 Submission

Submit your lab files to Brightspace for marking. Be ready to show : Output of: cat username-lab1a.py username-lab1b.py username-lab 1 c.py username- lab1d.py Output of username-lab 1 a.py username-lab1b.py username-lab 1 c.py username-lab1d.py File username-lab1e.ipynb Be able to answer any questions about the lab to show that you understood it.