User:Praveer-mathur-9f3d@uni-bonn.de/intro-to-programming-with-python

From HPC Wiki
Jump to navigation Jump to search

Short Quiz: Variable Assignment

What values do the two variables "mass" and "age" have in the following lines?

mass = 47.5
print(mass)

Click and submit to see the answer

age = 122
print(mass, age)

Click and submit to see the answer

mass = mass * 2.0
print(mass, age)

Click and submit to see the answer

age = age - 20
print(mass, age)

Click and submit to see the answer

Short Quiz: Data Types

planet = 'Earth'
What datatype does the variable planet have?

Integer
String
Boolean
Float

apples = 5
What datatype does the variable apples have?

Integer
String
Boolean
Float

distance = 10.5
What datatype does the variable distance have?

Integer
String
Boolean
Float

false = 7 > 2
What datatype does the variable false have?

Integer
String
Boolean
Float

Short Quiz: Boolean Operations

var1 = True
var2 = False
What is the value of each variable after each of the following print statements?

print(var1, var2)

Click and submit to see the answer

var2 = var1 or var2
print(var1, var2)

Click and submit to see the answer

var1 = var1 and not var2
print(var1, var2)

Click and submit to see the answer

var1 = not(var1 and not var1)
print(var1, var2)

Click and submit to see the answer

Short Quiz: String Concatenation

str1 = "Uni"
str2 = "Bonn"
str3 = str2 + str1
str4 = str1 * 3

What output do you expect after doing print(str3)?

Click and submit to see the answer

What output do you expect after doing print(str4)?

Click and submit to see the answer

Short Quiz: Containers

Read the following code:

uni = {"departments": ["physics", "chemistry"],
"employees": 4000,
"campuses": ("Poppelsdorf", "Endenich"),}

What do the following lines print? Keep in mind that the lines could also give an error output.

print(uni[0])

Click and submit to see the answer

print(uni["departments"][0]

Click and submit to see the answer

print(uni["employees"] = 5000

Click and submit to see the answer

print(uni["campuses"][0] = "Innenstadt"

Click and submit to see the answer

print(uni["campuses"][0]

Click and submit to see the answer

Short Quiz: Understanding if Statements

Which of the following statements would get printed?

if '':
print('empty string is true')

Yes
No


if 'word':
print('word is true')

Yes
No


if []:
print('empty list is true')

Yes
No


if [1, 2, 3]:
print('non-empty list is true')

Yes
No


if 0:
print('zero is true')

Yes
No


if 1:
print('one is true')

Yes
No

Short Quiz: Understanding for Loops

Consider the following Python code:
numbers = [5, 8, -3, 6, 0, -2]
summed = 0
for num in numbers:
    if num < 0:
        continue
    summed = summed + num
    print(summed)


What does the output look like after the loop is done running?

Click and submit to see the answer

Short Quiz: Mutability

Will the following lines work or result in an error?

numbers = [5, 8, -3, 6, 0, -2]
numbers[3] = 1

Will work
Will result in an error

string = "banana"
string[0] = "c"

Will work
Will result in an error

languages = ("python", "C", "Fortran")
languages[1] = "C++"

Will work
Will result in an error

alice = {"first_name": "Alice", "age": 32}
alice["age"] = 33

Will work
Will result in an error

Short Quiz: Understanding Slicing

What will the following statements print?

string = "Uni Bonn"
print(string[5])

Click and submit to see the answer

string = "Uni Bonn"
print(string[0:2])

Click and submit to see the answer

string = "Uni Bonn"
print(string[5:])

Click and submit to see the answer