18
loading...
This website collects cookies to deliver better user experience
print(“Hello World”)
python3 hello.py
Hello World
#single line comment
’’’
Multi-line comment
using docstring
’’’
fav_activity = “swimming”
print(fav_activity)
print(46 + 2) # 48 (addition)
print(10 - 9) # 1 (subtraction)
print(3 * 3) # 9 (multiplication)
print(84 / 2) # 42.0 (division)
print(2 ** 8) # 256 (exponent)
print(11 % 2) # 1 (remainder of the division)
print(11 // 2) # 5 (floor division)
language = “python”
multi_lin = ’’’come one
came all
’’’
greetings = ‘hello’
# can store any data type
multiple_types = [True, 3.7, "Python"]
# access and modify
favourite_foods = ["pasta", "pizza", "french fries"]
print(favourite_foods[1]) # pizza
favourite_foods[0] = "rösti"
print(favourite_foods[0]) # rösti
# subsets
print(favourite_foods[1:3]) # ['pizza', 'french fries']
print(favourite_foods[2:]) # ['french fries']
print(favourite_foods[0:2]) # ['rösti', 'pizza']
# append
favourite_foods.append("paella")
# insert at index
favourite_foods.insert(1, "soup")
# remove
favourite_foods.remove("soup")
# get length
print(len(favourite_foods)) # 4
# get subset (the original list is not modified)
print(favourite_foods[1:3]) # ['pizza', 'french fries']
# lists inside lists
favourite_drinks = ["water", "wine", "juice"]
favourites = [favourite_foods, favourite_drinks]
print(favourites[1][2]) # juice
#tuples
new_tuple = ("a", "b", "c", "d")
print(len(new_tuple)) # 4
print(new_tuple[1]) # b
print(new_tuple[1:4]) # ('b', 'c', 'd')
t1 = tuple([12,25,93])
t2 = tuple('abcde')
t1 = (5,)
language_creators = {
"Python" : "Guido van Rossum",
"C" : "Dennis Ritchie",
"Java" : "James Gosling",
"Go": "Robert Griesemer",
"Perl" : "Larry Wall"
}
# access, modify, delete
print(language_creators["Python"]) # Guido van Rossum
language_creators["Go"] = ["Robert Griesemer", "Rob Pike", "Ken Thompson"]
print(language_creators["Go"]) # ['Robert Griesemer', 'Rob Pike', 'Ken Thompson']
print(len(language_creators)) # 5
del language_creators["Perl"]
print(len(language_creators)) # 4
# print keys and values
print(language_creators.keys())
print(language_creators.values())
# booleans
a = True
b = False
if a is True and b is False:
print("YUP!")
# logical operators:
laundry_day = "Monday"
today = "Tuesday"
on_vacation = False
if today is laundry_day and today is not "Sunday" and on_vacation is False:
print("It's laundry day!")
else:
print("The laundry can wait!")
# 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")
# foor loop
for x in range(0, 3):
print(x)
# loop through list
for x in ["Python", "Go", "Java"]:
print(x)
# loop through dictionary
language_creators = {
"Python" : "Guido van Rossum",
"C" : "Dennis Ritchie",
"Java" : "James Gosling",
}
for key, value in language_creators.items():
print("Language: {}; Creator: {}".format(key, value))
# while loop
level = 0
while(level < 30):
level += 1
print(level)
# functions
def allowed_to_drive(age):
if age >= 21:
return True
else:
return False
print(allowed_to_drive(42)) # True
print(allowed_to_drive(12)) # False
def is_laundry_day(today, laundry_day = "Monday", on_vacation = False):
if today is laundry_day and today is not "Sunday" and on_vacation is False:
return True
else:
return False
print(is_laundry_day("Tuesday")) # False
print(is_laundry_day("Tuesday", "Tuesday")) # True
print(is_laundry_day("Friday", "Friday", True)) # False
# classes
class Email:
# __ means private
__read = False
def __init__(self, subject, body):
self.subject = subject
self.body = body
def mark_as_read(self):
self.__read = True
def is_read(self):
return self.__read
def is_spam(self):
return "you won 1 million" in self.subject
e = Email("Check this out", "There are a bunch of free online course here: https://course.online")
print(e.is_spam()) # False
print(e.mark_as_read())
print(e.is_read()) # True
# inheritance
class EmailWithAttachment(Email):
def __init__(self, subject, body, attachment):
super(EmailWithAttachment, self).__init__(subject, body)
self.__attachment = attachment
def get_attachment_size(self):
return len(self.__attachment)
email_with_attachment = EmailWithAttachment("you won 1 million dollars", "open attachment to win", "one million.pdf")
print(email_with_attachment.is_spam()) # True
print(email_with_attachment.get_attachment_size()) # 15
# write
todos_file = open("todos.txt", "wb")
todos_file.write(bytes("buy a new domain name\n", "UTF-8"))
todos_file.write(bytes("work on the side project\n", "UTF-8"))
todos_file.write(bytes("learn a new programming language\n", "UTF-8"))
todos_file.close()
# read
todos_file = open("todos.txt", "r+")
todos_file_contents = todos_file.read()
print(todos_file_contents)