Practise #01
Assignment 01
-
In the below elements which of them are values or an expression?
eg:- - values can be integer or string and - expressions will be mathematical operators.
1 2 3 4 5 6 7
* 'hello' -87.8 - / + 6
-
What is the difference between string and variable?
- Describe three different data types.
- What is an expression made up of? What do all expressions do?
- This assignment statements, like spam = 10. What is the difference between an expression and a statement?
-
After running the following code, what does the variable bacon contain?
1 2
bacon = 22 bacon + 1
-
What should the values of the following two terms be?
1 2
"spam" + "spamspam" "spam" * 3
-
Why is eggs a valid variable name while 100 is invalid?
- What three functions can be used to get the integer, floating-point number, or string version of a value?
-
Why does this expression cause an error? How can you fix it?
1
'I have eaten ' + 99 + ' burritos.'
Assignment 02
- What are the two values of the Boolean data type? How do you write them?
- What are the three different types of Boolean operators?
- Make a list of each Boolean operator's truth tables (i.e. every possible combination of Boolean values for the operator and what it evaluate).
- What are the values of the following expressions?
- What are the six comparison operators?
- How do you tell the difference between the equal to and assignment operators? Describe a condition and when you would use one.
-
Identify the three blocks in this code:
1 2 3 4 5 6 7 8 9
spam = 0 if spam == 10: print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam') print('spam')
-
Write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints Greetings! if anything else is stored in spam.
- If your programme is stuck in an endless loop, what keys you’ll press?
- How can you tell the difference between break and continue?
- In a for loop, what is the difference between range(10), range(0, 10), and range(0, 10, 1)?
Assignment 03
- Why are functions advantageous to have in your programs?
- When does the code in a function run: when it's specified or when it's called?
- What statement creates a function?
- What is the difference between a function and a function call?
- How many global scopes are there in a Python program? How many local scopes?
- What happens to variables in a local scope when the function call returns?
- What is the concept of a return value? Is it possible to have a return value in an expression?
- If a function does not have a return statement, what is the return value of a call to that function?
- How do you make a function variable refer to the global variable?
- What is the data type of None?
- What does the sentence import areallyourpetsnamederic do?
- If you had a bacon() feature in a spam module, what would you call it after importing spam?
- What can you do to save a programme from crashing if it encounters an error?
- What is the purpose of the try clause? What is the purpose of the except clause?
Assignment 04
- What exactly is []?
-
In a list of values stored in a variable called spam, how would you assign the value 'hello' as the third value? (Assume [2, 4, 6, 8, 10] are in spam.)
Info
Let's pretend the spam includes the list ['a', 'b', 'c', 'd'] for the next three queries.
-
What is the value of
spam[int(int('3' * 2) / 11)]
? - What is the value of
spam[-1]
? -
What is the value of
spam[:2]
?Info
Let's pretend bacon has the list [3.14, 'cat', 11, 'cat', True] for the next three questions.
-
What is the value of
bacon.index('cat')
? - How does
bacon.append(99)
change the look of the list value in bacon? -
How does
bacon.remove('cat')
change the look of the list in bacon? -
What are the list concatenation and list replication operators?
- What is difference between the list methods append() and insert()?
- What are the two methods for removing items from a list?
- Describe how list values and string values are identical.
- What's the difference between tuples and lists?
- How do you type a tuple value that only contains the integer 42?
- How do you get a list value's tuple form? How do you get a tuple value's list form?
- Variables that contain list values are not necessarily lists themselves. Instead, what do they contain?
- How do you distinguish between copy.copy() and copy.deepcopy()?
Assignment 05
- What does an empty dictionary's code look like?
- What is the value of a dictionary value with the key 'foo' and the value 42?
- What is the most significant distinction between a dictionary and a list?
- What happens if you try to access spam['foo'] if spam is {'bar': 100}?
- If a dictionary is stored in spam, what is the difference between the expressions 'cat' in spam and 'cat' in spam.keys()?
- If a dictionary is stored in spam, what is the difference between the expressions 'cat' in spam and 'cat' in spam.values()?
-
What is a shortcut for the following code?
1 2
if 'color' not in spam: spam['color'] = 'black'
-
How do you "pretty print" dictionary values, using which module and function?