Reference
while True:
print('You are nice!')
answer = input('Should I stop saying that? Write yes in that case.')
if answer == 'yes':
break
i = 0
while i < 100:
if i % 2 == 0: # continue with the next repetition if i is divisible by 2
continue
print(i,'is an odd number')
temp = int(input('What is the temperature?'))
if temp > 20:
print('Warm and nice!')
elif temp > 0: #if temp <= 20 and temp > 0
print('Not very cold, but not so warm either.')
number = int(input('Enter an integer'))
if number == 0:
print('You entered the number 0.')
elif number > 0:
print('You entered a positive number.')
else:
print('You entered a negative number.')
age = int(input('How old are you?'))
if age >= 18:
print('You are an adult!')
else:
print('You are not an adult.')
float('0.01') #converts the string '0.01' to 0.01
float(10) #converts the integer 10 to 10.0
number = 10
if number == 10:
print('The number is equal to 10.') #this will be printed
number = int(input('Enter an integer'))
if number == 0:
print('You entered the number 0.')
elif number > 0:
print('You entered a positive number.')
else:
print('You entered a negative number.')
answer = input('What is your name?') #The user's answer is saved in the variable answer
int(42) #converts the string '42' to 42
int(3.94) #converts the decimal number 3.94 to 3
print('Hello World') #prints "Hello World"
print(1+1) #prints the number 2
print(x) #prints the content of variable x
print('Hello World', end='') #prints "Hello World" without a new line
print('Hej','på','dig') #Prints "Hello to you"
print('Hej','på','dig',sep=' | ',end='!') #prints "Hello | to | you!" without a new line at the end.
from random import randint
roll = randint(1,6) #a random number 1-6
round(7.89) #rounds to 8
round(1.23) #rounds to 1
round(1.23456,2) #rounds 1.23
round(1.23456,4) #rounds to 1.2346
str(2.71) #converts the decimal number 2.71 to the string '2.71'
str(10) #converts the integer 10 to '10'
age = 15
print('You are ' + str(age) + ' years old.')
n = int(input('How many numbers do you want to print?'))
i = 1
while i <= n:
print(i)
i = i + 1
a = abs(5) # a = 5
b = abs(-5.2) # b = 5.2
lista = [1,2,3]
lista.append(4) # now [1,2,3,4]
lista = ["ett",2,3.0] # in Python, a list can consist of different types
lista.append([1,2,3,4]) # now ["one",2,3.0,[1,2,3,4]]
def print_hello():
print('Hello!')
def hello_name(name):
print('Hello ' + name)
hello_name('Tanja') #Prints "Hello Tanja"
hello_name('Emil') #Prints "Hello Emil"
def perimeter_rectangle(width, height):
perimeter = 2*width + 2*height
return perimeter
for i in range(10): # i goes from 0 to 9
print(i) # runs ten times
for character in 'pythonlabbet.se':
print(character)
for element in [1,3,3,7]:
print(element)
lista = [1,2,3]
length = len(lista) # length = 3
country = 'Sverige'
length = len(country) # length = 7
tuple = ('Sverige', 22472, 'Lund')
length = len(tuple) # length = 3
lista = [1,2,3,4]
largest = max(lista) # largest = 4
max(a,0) # 0 if a is negative, otherwise a
lista = [1,2,3,4]
smallest = min(lista) # smallest = 1
min(a,0) # 0 if a is positive, otherwise a
lista = [1,2,3]
lista.pop() # now [1,2]
lista = [1,2,3]
lista.pop(0) # now [2,3]
def square_and_add(x,y):
return x**2 + y**2
result = square_and_add(5,7)
print('Squaring and adding 5 and 7 gives', result)
lista = [1,2,3,4]
sum_val = sum(lista) # sum_val = 10
tupel = (1,2,3)
sum_val = sum(tupel) # sum_val = 6
back(100) #the turtle moves 100 steps backward
#draw a filled circle
begin_fill()
circle(50)
end_fill()
circle(100) #draws circle with radius 100
color('blue') #draw with blue
colormode(255)
color(240, 160, 80) #draw with orange
#draw a filled circle
begin_fill()
circle(50)
end_fill()
forward(100) #the turtle moves 100 steps forward
goto(-100,100) #go to (x,y) = (-100,100)
hideturtle() #hides the turtle
left(90) #the turtle turns 90 degrees left
pendown() #the pen is now down
penup() #the pen is now up
right(90) #the turtle turns 90 degrees right
shape() #default: a small arrow
shape('turtle') #turtle
shape('arrow') #large arrow
shape('circle') #circle
shape('square') #square
Screen().setup(800,600) #creates drawing area 800x600
speed(5) #sets drawing speed to 5
-- Output from your program will be here --