Tasks Flashcards

1
Q

Print a string three times

A

b = “hej”
c = b*3
print(c)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a program where the user can type in three words and using the format method, fit those words in to a pre written sentence then print.

A

in1 = input(“Type a animal :”)
in2 = input(“Type a colour :”)
in3 = input(“Type a name :”)

r = “The {} was painted {} by a guy named {}”.format(in1,in2,in3)
print(r)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create a string in all “CAPS” and then turn them into lower case letters with the lower function

A

r = “ALL CAPS”.lower()
print(r)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a program that prints a message if a variable is less than 10, and a different message if the variable is greater than or equal to 10

A

def f():
age = input(“type a number :”)
age = int(age)
if age <10:
print(“thats less than 10”)
elif age >=10:
print(“thats equal to or more than 10”)
f()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program that prints a message if a variable is less than 10, another message if the variable is greater than 10 but less than 25, and another message if the variable is greater than 25.

A

def f():
f_input = input(“Type a number :”)
f = int(f_input)
if f <=10:
print(“Thats less than 10”)
elif f >10 and f<=25:
print(“Thats between 10 and 25”)
elif f>25:
print(“Thats more than 25”)
f()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create a program that divides to variables and prints the remainder(%)

A

def f():
f = 14
b = 4
c = f%b
print(c)

f()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a program that takes two variables, divides them, and prints the quotient (//)

Facts : quotient är hur många gånger till exempel 10 kan delas med 5 vilket blir 2.

A

f = 10//5
print(f)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a program with a variable age assigned to an integer that prints different strings depending on what integer age is.

A

def age():
age = input(“Type a number :”)
age = int(age)
if age <=20:
print(“Thats young!”)
elif age >=20:
print(“Thats old!”)

age()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function that takes a number as an input and returns that number squared (**) = (upphöjt till)

A

def a():
a = input(“Type a number :”)
a = int(a)
print(a**a)

a()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a function that accepts a string as a parameter and prints it

A

def function(string):
print(string)
function(“Testing, 1, 2, 3. done!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a function that takes three required parameters and two optional parameters.

A

def f(x,y,z=2):
return x+y+z
result = f(10,15,25)
print(result)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a program with two functions. The first function should take an integer as a parameter and return the result of the integer divided by 2. The second function should take an integer as a parameter and return the result of the integer multiplied by 4. Call the first function, save the result as a variable, and pass it as a parameter to the second function.

A

def f_number():
f_number = input(“Type a number :”)
f_number = int(f_number)
z = (f_number) / 2
return z

def f_lina(result):
b = int(result)
b = (result)*4
print(b)

result = f_number()
f_lina(result)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a function that converts a string to a float and returns the result. Use exception handling to catch the exception that could occur.

A

def convert(string):
try:
return float(string)
except ValueError:
print(“Could not convert the string to a float”)

c = convert(“459”)
print(c)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a list of your favourite fruits

A

favorit_frukter = [“Äpple”,”Banan”,”apelsin”]
print(favorit_frukter)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a list of tuples, with each tuple containing the longitude and latitude of somewhere you’ve lived or visited.

A

kordinater = []

landet = [“0.7.5.7.8.5.4.5.6.3.1.1.0.0.4,”]
spånga = [“3.0.0.6.3.5.2.1.1.7.7.8.0.0,”]

kordinater.append(landet)
kordinater.append(spånga)

print(kordinater)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use the function “object” (.replace)
To switch a letter in a string to another one

A

r = “Kan man använda mankrankan allt?”
r = r.replace(“a”,”@”)
print(r)

17
Q

Use the function .index to find the first occurrence of a character in a string.

A

f = “Gammelfar”.index(“r”)
print(f)

18
Q

Write a program that collects two strings from a user, inserts them into a string of choice {} and prints a new string.

A

in1 = input(“Type an object: “)
in2 = input(“Type a receiver: “)

r = “"”Yesterday i wrote a {} and sent it to
{}””“.format(in1,in2)
print(r)

19
Q

Take the string “Where now? Who now? When now?” And call a method that returns a list that looks like :
[Where now, Who now, When now,]

A

w = “"”Where now? Who now? When now?””“.split(“?”)
print(w)

20
Q

Create a dictionary that contains different attributes about you: Height, favourite colour and favourite author.

A

gabriel = {“height”:”170cm”,”favourite author”:”diamant salihu”,”favourite colour”:”black and white”}

m = input(“Ask for either gabriels: height, favourite author or favourite colour. “)

if m in gabriel:
galleri = gabriel[m]
print(galleri)
else:
print(“hittade inget”)

21
Q

Take the list [“gabriel”,”är”,”en”,”stjärna”,”.”]
And return a gramatically correct string.

A

lama = [“gabriel”,”är”,”en”,”stjärna”,”.”]
lama = “ “.join(lama)
lama = lama[0: -2] + “.”
print(lama)

22
Q

Use a method to find the first index of the character «m» in the string «Hemingway»

A

m = “Hemingway”.index(“m”)
print(m)

23
Q

Create the string «three three three» with addition and then again using multiplication

A

m = “Three” + “ “ + “Three” + “ “ + “Three”
b = “Three “ *3
print(m)
print(b)

24
Q

Slice the string «Ganska bra jobbat, sa den lille mannen» to include only the characters before the comma.

A

m = “ganska bra jobbat, sa den lille mannen”
slice = m[0:17]
print(slice)

25
Q

Write a program that keeps giving the user questions till the user hits a special key to break

A

qs = [“Vem e han?”,”Varför e han?”,”Hur e han?”]
n = 0
while True:
print(“type q to quit”)
a = input(qs[n])
if a == “q”:
break
n = (n + 1) % 3