Tasks Flashcards
Print a string three times
b = “hej”
c = b*3
print(c)
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.
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)
Create a string in all “CAPS” and then turn them into lower case letters with the lower function
r = “ALL CAPS”.lower()
print(r)
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
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()
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.
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()
Create a program that divides to variables and prints the remainder(%)
def f():
f = 14
b = 4
c = f%b
print(c)
f()
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.
f = 10//5
print(f)
Write a program with a variable age assigned to an integer that prints different strings depending on what integer age is.
def age():
age = input(“Type a number :”)
age = int(age)
if age <=20:
print(“Thats young!”)
elif age >=20:
print(“Thats old!”)
age()
Write a function that takes a number as an input and returns that number squared (**) = (upphöjt till)
def a():
a = input(“Type a number :”)
a = int(a)
print(a**a)
a()
Create a function that accepts a string as a parameter and prints it
def function(string):
print(string)
function(“Testing, 1, 2, 3. done!”)
Create a function that takes three required parameters and two optional parameters.
def f(x,y,z=2):
return x+y+z
result = f(10,15,25)
print(result)
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.
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)
Write a function that converts a string to a float and returns the result. Use exception handling to catch the exception that could occur.
def convert(string):
try:
return float(string)
except ValueError:
print(“Could not convert the string to a float”)
c = convert(“459”)
print(c)
Create a list of your favourite fruits
favorit_frukter = [“Äpple”,”Banan”,”apelsin”]
print(favorit_frukter)
Create a list of tuples, with each tuple containing the longitude and latitude of somewhere you’ve lived or visited.
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)