21
loading...
This website collects cookies to deliver better user experience
Simplicity: Think less of the syntax of the language and more of the code.
Open Source: A powerful language and it is free for everyone to use and alter as needed.
Portability: Python code can be shared and it would work the same way it was intended to. Seamless and hassle-free.
Being Embeddable & Extensible: Python can have snippets of other languages inside it to perform certain functions.
Being Interpreted: The worries of large memory tasks and other heavy CPU tasks are taken care of by Python itself leaving you to worry only about coding.
Huge amount of libraries: Data Science? Python has you covered. Web Development? Python still has you covered.
Object Orientation: Objects help breaking-down complex real-life problems into such that they can be coded and solved to obtain solutions.
print('Hello, World!')
Python3 app.py
Hello, World!
# single line comment
'''
multiline comment
Using docstring
'''
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)
favourite_food = "Rösti"
print(favourite_food)
favourite_food = "Rösti"
print(favourite_food)
favourite_food = 26
print(favourite_food)
favourite_food = False
print(favourite_food)
# 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")
#Example 1:
language = "Python"
#Example 2:
multiline_str = '''
one
per
line'''
#Example 3, escape characters:
escape_str = "She said: \"Python is great!\""
print(escape_str) # She said: "Python is great!"
# Example 4, concatenate strings:
one = "Lux"
two = "Academy"
print(one + " " + two) # Lux Academy
# Example 5: interpolation.
# method 1
first_name = "Lux"
last_name = "Academy"
greet = f"Welcome at {first_name} {last_name}!"
print(greet) # Welcome at Lux Tech Academy!
# method 2
first_name = "Lux"
last_name = "Academy"
greet = 'Welcome at {} {}!'.format(first_name, last_name)
print(greet) # Welcome at Lux Tech Academy!
# method 3
first_name = "Lux"
last_name = "Academy"
greet = 'Welcome at{first} {last} !'.format( first=first_name, last=last_name)
print(greet) # Welcome at Lux Tech Academy!
#Example 6, extract substrings
name = "Monty Python"
print(name[6:9]) # Pyt
print(name[6:]) # Python
print(name[:5]) # Monty
age = 18 # int
pi = 3.14 # float
total = age + pi # float
print(type(age), type(pi), type(total)) # <class 'int'> <class 'float'> <class 'float'>
# booleans
a = True
b = False
if a is True and b is False:
print("YAY!")
# 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')
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())
# 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 < 10):
level += 1
# 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)