Chapter 2 notes 3 Flashcards
What uses a condition to decide whether to execute a statement (or a group of statements) with these in comparison operator
Using if statements
If the condition in a given statement is True, the corresponding ‘print’ statement executes; otherwise, it’s skipped
When do you use a script?
When you have many statements to execute as a group, you write them as a script stored in a file with the .py extension
What’s another word for script?
Programs
What are integrated development environments? (IDEs)
They enable you to choose whether to display line numbers
What does it mean when a line begins with a #?
A # symbol indicates that the rest of the line is a comment
Why would you insert comments to a code?
To improve readability. Also helps other programmers read and understand your code. They do NOT cause the computer to perform any actions when the code executes
A comment can begin to the right of the code on a given line and continue until the end of that line. Such a comment documents the code to its left
Docstrings, what are they?
A Docstrings explains the scripts purpose and begins. Ex:
“””Comparing integers using if statements and comparison operators.”””
They are usually found at the beginning of the script.
Blank lines, why?
You use blank lines and space characters to make code easier to read. Together, blank lines, space characters and tab characters are known as white space.
Python ignores them but some identification is required
How to keep a string together when using more than one line?
You may spread a lengthy statement over several lines with the \ continuation character. Python also allows you to split long code lines in parenthesis without using continuation characters.
Suite, explained and common syntax error
An if statement is the condition to test, and a colon (:) is followed by an intended body called a suite. Each suite must contain one or more statements. Forgetting the colon (:) after the condition is a common syntax error.
Python requires you to indent the statements in suites.
In if statements, = and ==
= is “assigned”
== is equal to
Why use chain comparisons?
To check a value is in range
In[1]: x = 3
In[2]: 1 <= x <= 5
Out[2]: True
In[3]: x = 10
In[4]: 1 <= x <= 5
Out[4]: False
You use __________ to document code and improve its readability
Comments
T/F the comparison operators evaluate left to right and all have the same level of precedence
False, the operators <, <=, > and >= all have the same level of precedence and evaluate left to right. The operators ++ and != have the same level of precedence and evaluate left to right. Their precedence is lower than that of <, <=, > and >=
For any of the operators !=, >= or <=, show that a syntax error occurs if you reverse the symbols in a condition
Use all 6 comparison operators to compare the values 5 and 9. Display the values on one line using print
In[2]: print(5 < 9, 5 <= 9, 5 > 9, 5 >= 9, 5 == 9, 5 != 9)
True True False False False True
How do you find out what type of input is considered?
In{1]: type(7)
Out[1]: int
You put the input inside the parentheses and press Enter
In[2]: type(2.5)
Out[2]: float
In[3]: type(‘dog’)
Out[3]: str
Assigning an object to a variable ______ (associates) that variable’s name to the object
Binds
In[4]: x = 7
In[5]: x + 10
Out[5]: 17
In[6]: x
Out[6]: 7
Python uses _______ ________ it determines the type of the object a variable refers to while executing your code.
Dynamic typing
What is garbage collection in Python?
When the memory in Python is re-written as an object is bound to another and what it was previously bound to has not been removed and forgotten. This helps ensure that memory is available for new objects you create.
Assigning an object to a variable _________ the variable’s name to the object
Binds
T/F a variable always references the same object
False, you can make an existing variable refer to a different object and even one of a different type
What is the type of expression 7.5 * 3?
Float