What will 'print(both)' output?
Click to see answer
False
Click to see question
What will 'print(both)' output?
False
How should range tests like a ≤ x < b be written in Python?
As (a <= x) and (x < b).
What data type represents true or false values in Python?
bool
What logical operator has higher precedence in the expression provided?
The 'and' operator has higher precedence than 'or'.
Why is indentation important in Python?
It determines the block of code that belongs to a conditional statement.
What type of input does the program ask from the user?
A number.
What does the program print when y equals 0?
It prints 'Y is zero.'
What is the recommended maximum line length in Python code?
Less than 80 characters.
Why do we use branching in programming?
To execute statements only if certain criteria are satisfied.
What Python function is used to get user input in the program?
input()
What is the result of 'a or b' when both a and b are False?
False.
What does an if-statement consist of?
A test expression and a body.
What is the result type of a logical operator?
Boolean (bool).
What does a evaluate to in control flow?
True or False.
What output is produced if the mark is between 70 and 75?
You get a B+.
What should the test expression (condition) evaluate to?
Either True or False.
What is the result of 'not a' when a is True?
False.
What types of data can be used to form a test expression?
int, float, string.
What are the relational operators listed?
<, <=, >, >=, ==, !=.
What is the output if the year is not a leap year?
The year isn't a leap year!
What is the result of 'a and b' when either a or b is False?
False.
Which logical operator represents OR?
or.
What is a test expression?
An expression formed by comparison or logical operations.
What will happen if y is zero in the provided program?
A run-time error will occur.
What is the purpose of checking if y is zero in the program?
To prevent a run-time error during division.
What does 'i < j' evaluate?
Evaluates to True if i is less than j, otherwise False.
What is a test expression in the context of logical operators?
An expression that can consist of sub-expressions combined using logical operators.
What does control flow refer to in programming?
Control flow determines the order in which individual statements, instructions, or function calls are executed.
What is the boolean data type?
The boolean data type represents two values: True and False.
What is a common mistake when using assignment and comparison operators?
Mixing '=' with '=='.
What is the correct way to add parentheses to show the precedence in the expression '17 x + 3 >= 9 or z != x * 2 and not(y / 2 < 3)'?
(17 x + 3 >= 9) or (z != (x * 2) and not(y / 2 < 3))
What does indentation refer to in Python?
The spaces at the beginning of a code line.
What does the 'not' operator do in the expression?
It negates the result of the expression that follows it.
Why might template files use fewer spaces for indentation compared to Python files?
Template files tend to have more levels of nesting.
What is the output if the year is a leap year?
The year is a leap year!
What type of input is expected for x and y in the code?
Integer input.
What constitutes a block of code in Python?
Code written at the same indentation level.
What will be printed if x is a positive odd number?
It is a positive odd number.
What are relational operators?
Operators used to compare values, such as ==, !=, <, >, <=, and >=.
What is the operator for exponentiation?
**.
What do comparison operations evaluate to?
Boolean (bool), either True or False.
What is the result of the comparison 'i > j'?
Evaluates to True if i is greater than j, otherwise False.
How does an if/else statement represent alternatives?
It represents the idea more naturally and clearly.
What does the 'if' part of an if/else statement do?
Executes the body if the test expression is true.
Which operator has lower precedence than 'and' but higher than 'or'?
= and !=
What will happen if 'count' is zero in the second program?
It will give an error.
How can the code be simplified using logical operators?
By combining conditions into a single if statement.
How many spaces are recommended for each indentation level in Python?
Four spaces.
What does the program prompt the user to input?
The year to be checked.
How does Python use indentation?
To indicate a block of code.
In the expression (expr1 and expr2), when is expr2 not evaluated?
When expr1 is false, making the whole expression false.
How does short-circuit evaluation simplify program code?
By avoiding unnecessary evaluations of sub-expressions.
What will be printed if x is a positive even number?
It is a positive even number.
What must be consistent in the body of an if-statement?
The indentation must be the same.
What type of numbers does the challenge extension require as input?
6 floating point numbers corresponding to the (x, y) coordinates of the triangle's corners.
What grade will you receive if your exam score is at least 70 and less than 75?
B+.
What operator has the highest precedence in the given expression?
not
In the expression, what does 'not(y / 2 < 3)' evaluate?
It evaluates to the negation of whether y divided by 2 is less than 3.
Why is indentation important in Python?
Indentation is used to define the scope of loops, functions, and conditionals in Python.
What happens if the is True?
Expressions in that block are evaluated.
What can you use to force the evaluation order in expressions?
Parentheses.
Which operators have the highest precedence?
Logical NOT.
What is an expression in programming?
An expression has a value and a type.
What is calculated when y is not equal to 0 in the provided code?
The value of x divided by y.
What happens if the condition in a branching statement is false?
The statement is skipped.
What is the main characteristic of sequential programs?
All statements are executed one-by-one.
What does the test expression 'y != 0' check?
Whether y is not equal to 0.
When is the body of an if-statement executed?
Only if the test expression evaluates to true.
What is the value of 'derive' in the example?
True
What output is given when the lengths 3, 4, and 5 are input?
This is a triangle!
What is the basic syntax of an if/else statement?
if test_expression_TE: body_to_be_executed_if_TE_is_true else: body_to_be_executed_if_TE_is_false.
What is the purpose of the program described in Exercise 2?
To check if three given lengths can form a triangle.
What does the 'else' part of an if/else statement do?
Executes the body if the test expression is false.
What should the program do when the user inputs an even number?
Print 'Even'.
Can the body of an if-statement be empty?
No, it can't be empty.
What is the significance of short-circuit evaluation in the first program?
It prevents division by zero by checking 'count != 0' first.
What is the purpose of a test expression in control flow?
To determine whether a statement should be executed based on a condition.
What does the variable 'isZero' represent in the expression 'isZero = (y == 0)'?
It checks if 'y' is equal to 0.
How many if-statement bodies are executed regardless of the value of y?
Only one if-statement body is executed.
What is the purpose of an if-statement?
To perform branching based on a test expression.
What are the three main logical operators mentioned?
and (AND), or (OR), and not (NOT).
What is the output of the code if y equals 0?
The code will not execute the division statement.
What is the condition to check if three lengths can form a triangle?
a + b > c, b + c > a, and c + a > b.
What is the syntax structure of an if-statement in Python?
if test_expression: followed by the body.
What is branching in programming?
Branching allows the program to take different paths based on conditions.
What does 'i == j' test for?
Tests for equality; True if i is the same as j.
What is the role of parentheses in logical expressions?
They clarify the order of operations and precedence.
What does the pass statement indicate in an if-statement?
That no actions should be done.
What is a potential problem in the provided code examples?
Dangling 'else' problem.
What happens if y is equal to 0 in the provided code?
The pass statement is executed, and no action is taken.
In the expression 'y / 2 < 3', what is being compared?
The result of 'y / 2' is being compared to 3.
What does the second if statement check in the provided code?
If x is even (x % 2 == 0).
Which logical operator represents AND?
and.
What should be considered when using short-circuit evaluation?
The order of the sub-expressions.
What logical operators are used in the leap year condition?
and, or
What does indentation refer to in Python?
The spaces at the beginning of a code line.
What will you receive if you fail in the quiz, exam, or do not hand in assignments?
A warning letter.
What does the condition 'mark >= 70 and mark < 75' check?
It checks if the mark is between 70 and 75.
What is the condition checked in both programs?
Whether 'scores / count' is less than 60 and 'count' is not zero.
What is the condition for a year to be a leap year?
A leap year is divisible by 4, but not divisible by 100, unless it is divisible by 400.
What happens when y is not equal to 0 in the provided code?
The program calculates and prints the value of x divided by y.
What is the result of 'not a' when a is False?
True.
What is the result of 'a and b' when both a and b are True?
True.
In the expression 'if (a and b)', what are 'a' and 'b'?
Variable names with Boolean values.
What should be avoided regarding blank lines in Python programs?
Excessive use of blank lines.
What will the code 'val = x / y' execute?
It will execute if y is not equal to 0.
How does Python identify the body of an if-statement?
By relying on indentation (the number of white spaces at the beginning of a line).
What does 'i >= j' check?
Checks if i is greater than or equal to j.
What is the value of 'drink' in the example?
False
What does 'both = derive and drink' evaluate to?
False
What is short-circuit evaluation?
Short-circuit evaluation is a programming technique where the second argument is evaluated only if the first argument does not suffice to determine the value of the expression.
What is the purpose of using logical operators in programming?
To simplify some program code.
What should the program do when the user inputs an odd number?
Print 'Odd'.
What is the purpose of an if/else statement?
To allow the program to choose only one of two alternatives.
What is the order of arithmetic operators in terms of precedence?
*, /, %, //, +, −.
What is short-circuit evaluation?
Sub-expressions in a test expression are not evaluated if the value of the entire expression is already known.
What logical operations can be used to form a test expression?
Logical operations on bools.
What are the only two possible values of the bool type?
True or False.
What is a block statement in an if-statement?
A block of code that executes multiple actions when the test expression is true.
What type of input does the program expect for x and y?
Integer input.
What is the value of 'pset_time' in the example?
15
How does Python indicate a block of code?
By using indentation.
What is the result of 'a or b' when either a or b is True?
True.
What type of values do logical operators take as input?
Boolean values (bool).
What output is given when the lengths 3, 4, and 15 are input?
This is NOT a triangle!
Are the two provided programs the same?
No, they are not the same.
What is the purpose of indentation in Python?
To determine when one line of code is connected to the line above it.
What is the main condition being checked in the program?
Whether the number is even or odd.
What is a block of code in Python?
A composed set of several statements to be executed when a certain condition is met.
What does the first if statement check in the provided code?
If x is greater than 0.
What is composed of several statements in an if-statement?
A block of code.
What is an example of a condition that can cause a run-time error?
If the variable y is zero.
What is the value of 'sleep_time' in the example?
8
What does 'print(sleep_time > pset_time)' evaluate to?
False
What are the two alternatives in an if/else statement?
The input number y is either equal to zero (y == 0 is true) or not equal to zero (y != 0 is true).
What does 'i <= j' signify?
Checks if i is less than or equal to j.
What is the correct way to add parentheses to the expression '18 x + 3 >= 9 or z != x * 2 and not(y / 2 < 3)'?
(((x + 3) >= 9) or ((z != (x * 2)) and (not((y / 2) < 3))))
What does 'i != j' indicate?
Indicates inequality; True if i is not the same as j.
What is the purpose of an if/else statement?
An if/else statement allows the program to execute one block of code if the condition is True and another block if it is False.
What are logical operators used for?
Logical operators are used to combine conditional statements.
What is an if-statement?
An if-statement executes a block of code if its condition evaluates to True.