22
loading...
This website collects cookies to deliver better user experience
Single Line comment:
#python is fun
Multiline comment:
'''
Python
is
fun
for
programming
'''
favourite_car = "Aston Martin"
print(favourite_car)
favourite_car = 26
print(favourite_car)
favourite_car = False
print(favourite_car)
# comparison operators:
age = 21
if age >= 21:
print("You can drive a trailer")
elif age >= 16:
print("You can drive a car")
else:
print("You can ride a bike")
# for loop
for x in range(2, 6):
print(x)
for x in range(2, 30, 3):
print(x)
# loop through list
for x in ["Python", "Go", "Java"]:
print(x)
# Nested for loops
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
# while loop
level = 0
while(level < 10):
level += 1
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
#####Example 1
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
#####Example 2
#Using object methods
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()