The only "True" language
def increment(x):
return x+1
result = increment(2)
print(result) # 3
def hello(name):
print("hello " + name)
hello("Max", 23) // error
def printPeople(*people):
print(people)
printPeople("Max", "Anna")
# ('Max', 'Anna')
# basic syntax:
lambda arugments: return-expression
double = lambda x: x * 2
print(double(2)) # 4
multiply = lambda x, y: x * y
print(multiply(2, 3)) # 6
age = 24
print("Max is " + age)
# error - we cannot combine datatypes in print()
age = 24
print("Max is " + str(age))
# 'Max is 24'
age = 24
text = "Anna is x years old"
print(text.replace("x", str(age)))
# 'Anna is 24 years old'
age = 24
name = "Max"
print(f"{name} is {age} years old")
# 'Max is 24 years old'
ages = [23, 21, 26]
for age in ages:
print(age)
# 23, 21, 26
for i in range(0, 3):
print(i)
# 0, 1, 2
ages = [23, 21, 26]
for i in range(0, 3):
print(ages[i])
# 23, 21, 26
ages = [23, 21, 26]
for i in range(0, len(ages)):
print(ages[i])
# 23, 21, 26
ages = [23, 21, 26]
counter = 0
while counter < 3:
print(ages[counter])
counter = counter + 1
# 23, 21, 26
for i in range (0, 4):
if i == 2:
continue
print(i)
# 0, 1, 3
for i in range (0, 4):
if i == 2:
break
print(i)
# 0, 1
name = input("Enter a name: ")
name # what the user entered
print(input("Enter a name: "))
print("Hello!") # "Hello!"
print(23)
print("Max is " + 23 ) # error
print("Max is " + str(23))
textFile = open("text.txt", "rt")
print(textFile.read())
# prints what is inside of the text.txt
ages = [23, 21, 26]
print(ages)
# [23, 21, 26]
# Lists can hold multiple data types:
randomData = ["car", 3.141, math.pi]
ages = [23, 21, 26]
ages[0] # 23
ages[1] # 21
ages[-1] # 26
ages[-2] # 21
ages = [23, 21, 26]
# adds a new element:
ages.append(29)
# [23, 21, 26, 29]
# removes the last element:
ages.pop()
# [23, 21, 26]
ages = [23, 21, 26]
print(ages[0:2])
# [23, 21]
# starting with 0, stopping
# one element before 2
ages = [23, 21, 26, 24, 34, 21, 31]
print(ages[:2])
# [23, 21]
print(ages[5:])
# [21, 31]
import time
print("now")
time.sleep(2)
print("after 2 seconds")