Introduction to Python Module 3 Python values variables
Introduction to Python: Module 3 Python values, variables, arrays, loops Lois Delcambre
Plan • Values, variables, and arithmetic in Python • Data structures and for loops
Strings – one kind of value trinket/Learn/Start Learning/Meet Tina Text – between single or double quotes – is called a string Where are the two strings in this program?
The two strings Meet Tina lesson Text – between single or double quotes – is called a string There are two strings in this program.
integers – another kind of value Meet Tina lesson Numbers – without a decimal point – are called integers Where are two integer values in this program?
The two integer values Meet Tina lesson Numbers – without a decimal point – are called integers There are two integer values in this program. Note: the two strings and the two integer values in this program are used as parameters. You can also have strings and integer values elsewhere.
Find the values in this program trinket/Learn/Start Learning/Color
There are 6 strings (red) and 4 integers (blue) Color lesson Note: numbers with a decimal point like 3. 1415 are called floating point numbers or floats.
You can do arithmetic with integers (or floats) (modification of Meet Tina lesson) Find the arithmetic in this program. + is addition - is subtraction * is multiplication / is division (the answer is always a float) ** is exponentiation
Two arithmetic expressions in this program (kind of silly – just an example) Meet Tina + is addition - is subtraction * is multiplication / is division ** is exponentiation Find the arithmetic in this program.
+ operator for strings Meet Tina + is concatentation – to “glue” two strings together * is used to make multiple copies of a string and concatenate them
* operator for strings Meet Tina * is used to make multiple copies of a string and concatenate them There are 3 copies of “Hello, ” written here.
In class exercise • trinket/Learn/Start Learning/Saying Hello lesson: – change some of the strings – change some of the integers – put some arithmetic in place of an integer constant (put arithmetic in place of 20) – modify the tina. write statement so that it concatenates some other text. – multiply your_name times some number in the tina. write command
Assignment statements introduce (or reuse) variable names (Meet Tina) This assignment statement introduces the variable name tina into the program. And it associates the value of the new turtle with this name.
Another example of an assignment statement (Saying Hello) What is the new variable name? And what is it’s value?
Answer (Saying Hello) What is the new variable name? your_name And what is it’s value? the string returned by the input function
When you use a variable name, Python substitutes its current value (Saying Hello) Here we see the variable name What gets written is the current value of this variable (plus the other text shown here, before and after).
Rules for variable names • Variable name may consist of letters, numbers and underscores. Must start with a letter or underscore. • Variable names are case sensitive. • Don’t use a Python reserved word; don’t use a name that’s already in your program for something else. • Descriptive names are very useful. • Avoid using the lowercase letter ‘l’, uppercase ‘O’, and uppercase ‘I’. Why? Because they are ambiguous.
Python reserved words they mean something special; don’t use them False class finally None continue for is lambda return try True def from nonlocal while and as assert break del elif else except global if import in not or pass raise with yield
a function definition also introduces a new name into your program Functions are recipes! This is a def command (with a block of code inside). The def command associates this function code with the name of this function, triangle. Note: function def statement does NOT run the code block. It just remembers this code block, if ever you invoke this function.
function invocation Functions are recipes! Following the def command, we can invoke the function. Try it the other way around. Try to invoke a function before you define it.
Example program • Which names are introduced in this program?
Which names are introduced in this program? rightward_square size t 1 https: //trinket. io/python/d 660 c 2 c 46 a
Where is size used? https: //trinket. io/python/d 660 c 2 c 46 a
size is used 4 times each time that size is used, Python substitutes the current value associated with the variable https: //trinket. io/python/d 660 c 2 c 46 a
Compare these 2 programs look for the variable called size https: //trinket. io/python/d 660 c 2 c 46 a
Here, we see a second assignment statement. It works like any other assignment statement, it sets the value of size. https: //trinket. io/python/d 660 c 2 c 46 a
What will these two programs draw? https: //trinket. io/python/d 660 c 2 c 46 a
Answer: the output from these two programs https: //trinket. io/python/d 660 c 2 c 46 a
Class activity • trinket/Learn/Start Learning/Going in Circles modify this program so that: – the eyes are the same size – there is a variable called size which starts at 15 • the eyeball is this size • the eye is triple this size • the mouth is double this size
One solution to the exercise modifying Going in Circles
Let’s look at the names for parameters used inside a function definition
When you define a function, you define how many values you expect (when someone invokes you). This function expects two parameters. And … these parameter names are used inside the block of code in the function – to refer to the values that arrive. t refers to a turtle and, this function goes forward according to the value of the second parameter called side
Notice: turtle has name t 1 in main but is called t inside the function.
Class activity trinket/Learn/Start Learning/Functions are Recipes • Modify the Functions are Recipes lesson: – add one parameter to the function definition to represent the length of one side of the triangle (choose whatever variable name you like) – whenever you invoke the function, make sure you supply one parameter – invoke the function more than once – with different values for the parameter
One possible answer (Functions are Recipes) (1) Output: parameter defined here Original Modified program
One possible answer (Functions are Recipes) (2) Output: parameter defined here parameter used here Original Modified program
One possible answer (Functions are Recipes) (3) Output: parameter defined here parameter used here Original Modified program parameter supplied here
Challenge question (Functions are Recipes) Why did I put this command in? Try leaving it out. What does the home method do to a turtle? Original Modified program
Challenge question – explanation (1) • the turtle has a heading – the direction the turtle is pointing • the turtle starts at (0, 0) pointing to the East (the right) • when a turtle turns left or right or when it draws certain things (like circles), the heading can change • Note: home method brings turtle to (0, 0) pointing East (to the right).
Challenge question answers (1) Why did I put this command in? So that both triangles are oriented the same way. What does the home method do to a turtle? Brings it to (0, 0) heading East (to the right). Original Modified program
Challenge question – explanation (2) • you can change the heading with the setheading method, like this: tina. setheading(degrees) • • 0 degrees – pointing right 90 degrees – pointing up 180 degrees – pointing right etc. • What happens if you do this: tina. setheading(360) ?
Challenge question – explanation (3) Original Modified program another way to modify the program use setheading 0 degrees is East (right)
Plan – Day 3 • Values, variables, and arithmetic in Python • Data structures and for loops
Lists hold multiple values We can use for loops with lists in Python
trinket/Learn/start learning Lists of Numbers lesson
Lists of Numbers lesson explained (1) We can build a list by putting multiple values in square brackets, separated by commas. This assignment statement associates the name number_list with the list on the right side.
Lists of Numbers lesson explained (2) The for loop in Python has a block of code. This for loop will associate the variable name number with the first item in the list and run the block of code. Then, it will associate the variable name number with the second item in the list and run the block of code, etc.
Lists of Numbers lesson explained (3) This for loop will associate the variable name number with the first item in the list and run the block of code. Then, it will associate the variable name number with the second item in the list and run the block of code. The turtles goes forward 10, then 20, then 30, then 40, … to 100.
Another example of a list the Changing Colors lesson trinket/Learn/Start Learning Changing Colors
Changing Colors lesson – explained (1) Here we see a list of strings – representing a list of colors. Each time through the block (inside the for loop), the each_color variable will have one of the values from the list.
Changing Colors lesson – explained (2)
Class activity • trinket/Learn/Start Learning/Changing Colors – choose different colors for the circles – use several of your colors more than once – change the order of the colors – use only four colors in the list • challenge question: why did the position of the circles change when you have fewer colors in the list?
Class activity – answer to challenge question • trinket/Learn/Start Learning/Changing Colors – choose different colors for the circles – use several of the colors more than once – change the order of the colors – use only four colors • challenge question: why did the position of the circles change when you have fewer colors in the list? Inside the for loop, the angle is set to 360/len(colors) len is a function built-in to Python that returns the number of items in the list.
Note: Changing Colors program can be modified and still have the same result (try it) Original program We can move the assignment statement before the for loop because the length of the list doesn’t change inside the loop. Modified program
Another kind of for loop a for loop can repeat a block of code a specific number of times compare
a def statement has a code block inside. the previous line ends with a colon and then the block is indented.
a def statement has a code block inside. a for loop, also has a code block inside. (as always) the previous line ends with a colon and then the code block is indented. This is an example of a code block inside another code block.
A for loop like this uses the range function in Python. This for loop will execute the block of code 4 times.
This for loop will set the variable name i to the value 0 and then execute the code block. Then i will be set to 1 and the code block will be executed. then i will be set to 2. And, finally, i will be set to 3. In this particular for loop, we don’t use the variable called i. We just want the code block to be executed 4 times.
Class activity • Modify the trinket/Learn/Start Learning/Make 5 Cakes so that is uses a for loop – either with range(3) to draw 3 cakes or range(5) to draw 5 cakes
One Solution I used an assignment statement to set a variable called xloc to -100. Then I invoked make_cake 4 times. each time, I added 100 to xloc before I called it again.
- Slides: 62