Chapter 2 notes 3 Flashcards

1
Q

What uses a condition to decide whether to execute a statement (or a group of statements) with these in comparison operator

A

Using if statements
If the condition in a given statement is True, the corresponding ‘print’ statement executes; otherwise, it’s skipped

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When do you use a script?

A

When you have many statements to execute as a group, you write them as a script stored in a file with the .py extension

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s another word for script?

A

Programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are integrated development environments? (IDEs)

A

They enable you to choose whether to display line numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does it mean when a line begins with a #?

A

A # symbol indicates that the rest of the line is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why would you insert comments to a code?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Docstrings, what are they?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Blank lines, why?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to keep a string together when using more than one line?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Suite, explained and common syntax error

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In if statements, = and ==

A

= is “assigned”
== is equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why use chain comparisons?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You use __________ to document code and improve its readability

A

Comments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F the comparison operators evaluate left to right and all have the same level of precedence

A

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 >=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

For any of the operators !=, >= or <=, show that a syntax error occurs if you reverse the symbols in a condition

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use all 6 comparison operators to compare the values 5 and 9. Display the values on one line using print

A

In[2]: print(5 < 9, 5 <= 9, 5 > 9, 5 >= 9, 5 == 9, 5 != 9)
True True False False False True

17
Q

How do you find out what type of input is considered?

A

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

18
Q

Assigning an object to a variable ______ (associates) that variable’s name to the object

A

Binds

In[4]: x = 7

In[5]: x + 10
Out[5]: 17

In[6]: x
Out[6]: 7

19
Q

Python uses _______ ________ it determines the type of the object a variable refers to while executing your code.

A

Dynamic typing

20
Q

What is garbage collection in Python?

A

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.

21
Q

Assigning an object to a variable _________ the variable’s name to the object

A

Binds

22
Q

T/F a variable always references the same object

A

False, you can make an existing variable refer to a different object and even one of a different type

23
Q

What is the type of expression 7.5 * 3?

A

Float

24
Q
A