With Many Thanks to Dr. Amber Settle for the Original Version of These Notes s1

Total Page:16

File Type:pdf, Size:1020Kb

With Many Thanks to Dr. Amber Settle for the Original Version of These Notes s1

CSC 241 NOTES Yosef Mendelsohn

With many thanks to Dr. Amber Settle for the original version of these notes. Prof Settle's notes in turn are based on the Perkovic text.

Getting started with Python The rest of this week will be spent learning some basic Python syntax.

The Python interpreter One way you can use Python is by using the command-line window. This will look something like:

You can then execute the classic first program (hello world) as follows:

>>> print("Hello world!") Hello world!

The function print() is not like a function in mathematics.

It takes input, for example the phrase Hello world!, and does something with it, specifically prints it to the screen.

Alternatively you can use the IDLE environment to run Python programs. The screen shot below shows an execution of the hello world program: We will mostly use IDLE for developing, running, and debugging Python programs. It provides more helpful features than the command-line window. Expressions One of the most fundamental things in any programming language is an expression.

The easiest types of expressions are mathematical expressions, which will have us using the Python interpreter like a calculator. To do this, we type an ordinary algebraic expression at the prompt and press enter to evaluate it:

>>> print(3+7) 10 Note: Simply typing an expression, will not only cause that expression to be executed, but IDLE (and related Python shell environments) will also typically output the result of that expression to the console:

>>> 3+7 10 Let's look at some more expressions:

>>> 12/5 2.4 >>> 12 // 5 2 >>> 12 % 5 2

2 >>> 3*2+1 7 >>> (3-1)*(4+1) 10 >>> 4.321/3+10 11.440333333333333 >>> 4.321/(3+10) 0.3323846153846154 >>> 2.75 2.75 >>> 2**3 8

Variables and Identifiers Like in algebra, values may be assigned to variables. For example, we can create a variable and then assign it a number.

Let's create a variable called 'age' and assign it a value of 65: >>> age = 65 >>> print(age) 65

Identifiers: An identifier is the name we give to something. Any time we name something (such as a variable) we must choose an identifier. Choose your identifiers wisely! Good code is clear code.

Suppose we wanted to have a variable in which we recorded the age at which people are allowed to consume alcohol. Which of the following identifiers do you think is best? >>> x = 21 >>> age = 21 >>> legal_drinking_age = 21

I hope you agree that the last one is far more clear.

That being said, when you are just practicing, you don't need to go overboard on this. When we are learning Python, I don't mind if you use identifiers such as 'x' or 'n1' and things like that. However, when writing code such as for your homework, you should always try to use clear identifiers!

Getting back to variables… >>> y = 21 >>> print(y) 21

>>> x >>> print(x)

3 Traceback (most recent call last): File "", line 1, in x NameError: name 'x' is not defined Note the initial error message above. The variable name x is undefined until it is assigned. Any attempt to use the variable before it is defined will result in an error. However, the code below will execute just fine:

>>> x = 3 >>> x 3

Note: Again, we do not necessarily have to type 'print(x)' above. In a live interpreting environment such as IDLE, Python will automatically output the result However, when you are executing a complete program, you should use the 'print' function if you wish to output information to the console.

Assignment Statements

The statement x = 4 is called an assignment statement.

An assignment statement has the following syntax: =

Important: Note that the assignment operator (=) here is not checking whether one value is equal to another. Instead, the '=' operator is assigning a value to the variable 'x'. (If you want to compare two things to see if they are identical, we use a different operator. We will discuss this shortly.)

In an assignment statement, the expression to the right of the assignment operator is evaluated and then the value of the expression is placed into the variable on the left side of the assignment operator.

Once a value has been assigned to a variable, the variable can be used in expressions.

When Python evaluates an expression containing a variable, it will evaluate the variable to its currently assigned value.

Consider the following example:

>>> x = 4 >>> 4*x 16 >>> y = 4*x >>> y

4 16

It is also possible to modify the value of an existing variable: >>> score = 83 >>> score = score + 3 >>> print(score) 86 >>> salary = 50000 >>> salary = salary + (0.1*salary) >>> print(salary) 55000.0

Let's say we have exam scores for 3 students and wish to calculate the average of those scores: >>> score1 = 87 >>> score2 = 93 >>> score3 = 62 >>> mean = (score1+score2+score3)/3.0 >>> print(mean) 80.66666666667 >>> print(score4) Traceback (most recent call last): File "", line 1, in score4 NameError: name 'score4' is not defined Note: In our example for 'mean' it was necessary to divide by 3.0 rather than 3. We will discuss the reason for this down the road.

CodeLab Accessing CodeLab: see this document

Note: Without a full license, CodeLab will limit you to 10 exercises. This will be enough for today, but by the next session, there will be many more than 10. So it is vital that you take care of setting up your CodeLab today.

CodeLab Terminology: CodeLab is very specific about terminology. When answering questions, you should be very careful about recognizing exactly what CodeLab is asking for. Therefore, as preparation for your first assignment, we need to familiarize ourselves with some CodeLab terminology.

A literal is a value. You can think of it as the simplest possible value that can be stored into a variable.

Examples:  12  -5  3.1415

5  ‘z’

An expression is a piece of programming code that evaluates to a literal. For example, an arithmetic expression such as 4*x + 2 is an expression, assuming that x is defined to hold an integer value.

A statement is a complete Python statement. For example, the assignment x = 5 would be a statement.

Note: If CodeLab asks you for a literal and you give it a statement, it will evaluate your answer is incorrect – even if the correct answer is present somewhere inside your statement! So be sure to give CodeLab exactly what it is asking for. Exercises For the remainder of the class session you will do some exercises to familiarize yourself with CodeLab and with basic expressions in Python.

When you submit, if the code turns green, it is telling you that it is correct. If it turns red, there is a problem. This will be accompanied by one or more error messages. Sometimes these are helpful, sometimes they are not.

Do the following exercises from the Week 1, Lecture 1 section of the CodeLab site:  51001  51013  51014  51043  51032  51030  51033  51037

Note: If you haven’t already signed up for CodeLab, you should now. Instructions are found on the 'Resources' page.

6

Recommended publications