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