In: Computer Science
Control Flow
1. What is the difference between 10 / 3 and 10 // 3?
2. What is the result of 10 ** 3?
3. Given (x = 1), what will be the value of after we run (x += 2)?
4. How can we round a number?
5. What is the result of float(1)?
6. What is the result of bool(“False”)?
7. What is the result of 10 == “10”?
8. What is the result of “bag” > “apple”?
9. What is the result of not(True or False)?
10. Under what circumstances does the expression 18 <= age < 65 evaluate to True?
Primitive Types
1. What is a variable?
2. What are the primitive built-in types in Python?
3. When should we use “”” (tripe quotes) to define strings?
4. Assuming (name = “John Smith”), what does name[1] return?
5. What about name[-2]?
6. What about name[1:-1]?
7. How to get the length of name?
8. What are the escape sequences in Python?
9. What is the result of f“{2+2}+{10%3}”?
10. What does name.strip() do?
11. How can we check to see if name contains “John”?
12. What are the 3 types of numbers in Python?
Solution:
1.
/ is used for floating point division , it will produce floating point value as result
// is used for integer division , it will produce the quotientas result
Therefore,
10 / 3 = 3.3333333333333335
10 // 3 = 3
2.
** is the exponentiation operator in python which returns the given power to its exponent.
10 ** 3 = 1000
3.
x += 2 is interpreted as x = x + 2
Therefore,
x = 1 + 2
x = 3
4.
To round a number in python we use the function round(number , digits) where number the value that we need to round and digits specifies upto how many digis we need to round the number.
round(9.456,2) --> 9.46
round(9.456,1) --> 9.5
5.
float() function returns the floating point representation of its argument.
float(1) returns 1.0
6.
Since "False" is a string and when passed as an argument to the bool function it is interpreted as True and the bool returns True
bool("False") returns True
7.
Since one argument is string and another argument is integer the result would be always False.
10 == "10" returns False
8.
When the strings bag and apples are placed in the alphabetical order apples comes first and then bag. bag is greater than apples in this order.
Therefore,
"bag" > "apples" returns True
9.
or operator returns True if either of its operands is True and not operator returns complement of its operands value.
Now ,
(True or False) returns True
not ( True ) returns False
10.
The expression 18 <= age < 65 evaluates to true if the value of age is in between 18 to 65 including 18 and excluding 65.
Therefore , the values of age which evaluates the expression to True are
18,19,20,--------------,63,64
I hope this would help........................:-))