focusNode
Didn't know it?
click below
Knew it?
click below
Embed Code - If you would like this activity on your web page, copy the script below and paste it into
your web page.
Normal Size Small Size show me how
Review of Python Chapter 2
2-1. What are the values for Boolean in Python? a. True, False b. TRUE, FALSE c. true, false d. Yes, No | a. True, False |
2-2. How many comparison operators are there in Python? | Six: ==, !=, >, >=, <, <= |
2-3. What is the difference between = and ==? | = is the assignment operator. It is used to assign a value to a variable. == is the equal comparison operator. It is used to compare if 2 values are equal. |
2-4. What are the logical operators in Python? | and, or, not |
2-5. Multiple Boolean expressions can be combined by using a ________ operator to create compound expressions. | logical |
2-6. When using the _________ operator, one or both sub-expressions must be true for the final expression to be true. | or |
2-7. When using the _________ operator, both sub-expressions must be true for the final expression to be true. | and |
2-8. Which of the following is the correct if clause to determine whether y is in the range 10 through 50? a. if 10 < y 50 b. if 10 < y or y > 50 c. if 10 > y and y < 50 d. if y > 10 and y < 50 e. if y > 10 or y < 50 | d. if y > 10 and y < 50 |
2-9. Is there another way to accomplish this comparison? if x > 10 and x < 20: | Yes. by using: if 10 < x < 20: |
2-10. What is the result of the following Boolean expression, if x is 5, y is 3, and z is 8? x < y or z > x | True |
2-11. What is the result of the following Boolean expression, if x is 5, y is 3, and z is 8? x < y and z > x | False |
2-12. What is the result of the following Boolean expression, if x is 5, y is 3, and z is 8? not (x < y or z > x) and y < z | False |
2-13. Which of the following is the correct if clause to use to determine whether choice is other than 10? a. if choice != 10: b. if choice != 10 c. if choice <> 10: d. if choice <> 10 e. None of the above | a. if choice != 10: |
2-14. A(n) ____________ statement will execute one block of statements if its condition is true, or another block if its condition is false. | if-else |
2-15. What Python keyword allows us to check for more conditions in case the first block was false? | The elif keyword (if-elif...else) |
2-16. The logical ____ operator reverses the truth of a Boolean expression. | not |
2-17. What will this code print? flag = False if not flag: print ('Line 1 will be printed') else: print ('Line 2 will be printed') | Line 1 will be printed |
2-18. What will this code print? x = 10 y = 15 if x%3==0 and y%3==0: print ('Line 1 will be printed') elif x%3==0 or y%3==0: print ('Line 2 will be printed') else: print ('Line 3 will be printed') | Line 2 will be printed |
Addition (+)
Description
This operator provides the sums of two numbers. Basic arithmetic operator used for addition; the result of an arithmetic operator is usually a numeric value.
Syntax
[expression1] <operator> [expression2]
Comments
Although the + operator can be used to concatenate two character strings, the & operator should be used for concatenation to
eliminate ambiguity and provide self-documenting code. If + operator is used, there may be no way to determine whether addition or string concatenation will occur. The underlying subtype of the expressions determines the behavior of the + operator in the following way:
Both expressions are numeric | Add |
Both expressions are strings | Concatenate |
One expression is numeric and the other is a string | Add |
If one or both expressions are Null expressions, the result is Null. If both expressions are Empty, the result is an integer subtype. However, if only one expression is Empty, the other expression is returned unchanged as a result.
Example
READ {.ProjectsSampleSample.prj}:Oswego DEFINE Newvar NUMERIC ASSIGN Newvar = Age + 5 LIST Age Newvar
ARITHMETIC
Description
These basic arithmetic operators can be used in combination with
other commands. The result is a numeric value.
Syntax
[Expression] <Operator> [Expression]
- [Expression] is a numeric value or a variable containing data in numeric format.
Comments
The results are expressed in numeric format. The basic mathematical operators that can be used in Epi Info are as follows:
- Addition + Basic arithmetic operator used for addition; the result of an arithmetic operator is usually a numeric value (i.e., EX. 3 + 3).
- Subtraction – (Used for subtraction or negation.) Basic arithmetic operator used for subtraction or negation; the result of an arithmetic operator is usually a numeric value (i.e., EX. 3 – 1).
- Multiplication * (Asterisk) Basic arithmetic operator used for multiplication; the result of an arithmetic operator is usually a numeric value.
- Division / Basic arithmetic operator used for division; the result of an arithmetic operator is usually a numeric value.
- Exponentiation ^
- Modulus or Remainder MOD
Arithmetic operators are shown in descending order of precedence. Parentheses can be used to control the order in which operators are evaluated. The default order, however, frequently achieves the correct result.
While it is possible to do date math with dates considered as a number of days (e.g., IncubationDays = SymptomDateTime – ExposureDateTime), the behavior of the database services underlying Epi Info makes it more efficient to use time interval functions (e.g., IncubationDays = MINUTES(ExposureDateTime, Symptom DateTime)/[24*60]). For doing date math, the following rules apply:
Date + Date produces Date
Date – Date produces Days
Date * Date not permitted
Date / Date not permitted
Date ^ Date not permitted
Date + Number produces Date
Number +
Date produces Number
The last two rules apply as well to other math operations: -, *, /, ^
The “zero day” for date math is December 30, 1899.
Example
READ {.ProjectsSampleSample.prj}:Surveillance DEFINE var1 NUMERIC ASSIGN var1=1250 MOD 100 DEFINE var2 NUMERIC ASSIGN var2=1+1 DEFINE var3 NUMERIC ASSIGN var3=2-1 DEFINE var4 NUMERIC ASSIGN var4=1*1 DEFINE var5 NUMERIC ASSIGN var5=8/4 DEFINE var6 NUMERIC ASSIGN var6=5^2 LIST var1 var2 var3 var4 var5 var6
Comparisons
Description
These comparison operators can be used in If, Then, and Select statements in Check Code and Analysis programs. Yes/No variables can only be tested for equality against other
Yes/No constants (+), (-), and (.).
= | Equal to Comparison operator used for equal to; the result of comparison operators is usually a logical value, either True or False. EX. A1 = B1. |
> | Greater than comparison operator. Compares a value greater than another value; the result of comparison operators is usually a logical value, either True or False. Comparison operator used for comparing a value greater than another value; the result of comparison operators is usually a logical value, either True or False. EX. A1 > B1. |
< | Less than comparison operator. Compares a value less than another value; the result of comparison operators is usually a logical value, either True or False. Comparison operator used for comparing a value less than another value; the result of comparison operators is usually a logical value, either True or False. EX. A1<B1. |
>= | Greater than or equal to. |
<= | Less than or equal to. |
<> | Not equal to. |
LIKE | Left side variable matches right side pattern; in pattern, ’*’ matches any number of characters, ’?’ matches any one character. |
Syntax
[Expression] <operator> [Expression]
[Expression] is any valid expression.
Comments
Comparison operators are executed from left to right. There is no hierarchy of comparison operators. The <> operator can be used only with numeric variables. For non-numeric variables, use NOT.
Example
READ {.ProjectsSampleSample.prj}:Surveillance SELECT Age>20 LIST Age Disease READ {.ProjectsSampleSample.prj}:Surveillance SELECT Age<45 LIST Age Disease READ {.ProjectsSampleSample.prj}:Surveillance SELECT Age>=38 LIST Age Disease READ {.ProjectsSampleSample.prj}:Surveillance SELECT Age<>77 LIST Age Disease