Last active
March 21, 2019 21:36
-
-
Save achillesp/737ef9d6be1fe77903bc22b04ea44891 to your computer and use it in GitHub Desktop.
Python coding examples. I/O, math, conditionals, loops and functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python Code Examples & Tutorials | |
########################################## | |
# 1. Basic Input/Output | |
########################################## | |
message = "Winter is coming!" | |
print(message) | |
first_name = "Tyrion" | |
last_name = "Lannister" | |
full_name = first_name + " " + last_name | |
print(full_name) | |
# this is a comment | |
# ask user's name | |
user_name = input("What is your name?") | |
print("You know nothing " + user_name) | |
########################################## | |
# 2. Math operations | |
########################################## | |
x = 2 + 3 | |
y = 2 * 3 | |
z = 3 / 2 | |
print(z) | |
print(x ** 2) | |
########################################## | |
# 3. Lists | |
########################################## | |
dragons = ['Drogon', 'Rhaegal', 'Viserion'] | |
print(dragons) | |
print(dragons[0]) | |
dragons.pop(1) | |
print(dragons) | |
dragons.append('Rhaegal') | |
print(dragons) | |
dragons.sort() | |
print(dragons) | |
print(len(dragons)) | |
########################################## | |
# 4. Conditionals | |
########################################## | |
a = 5 | |
b = 7 | |
c = 5 | |
d = 9 | |
e = 5 | |
if a > b: | |
print(a) | |
else: | |
print(b) | |
if a == b: | |
print("a equals b") | |
elif a == c: | |
print("a equals c") | |
elif a == d: | |
print("a equals d") | |
else: | |
print("a equals none") | |
if a == b or a == c or a == d: | |
print("a equals something") | |
if a == c and a == e: | |
print('a equals c and e') | |
if a != b: | |
print("a not equal to b") | |
########################################## | |
# 5. Loops | |
########################################## | |
houses = ['Stark', 'Lannister', 'Targaryen', 'Baratheon', 'Tyrell', 'Tully', 'Martell', 'Greyjoy', 'Arryn'] | |
for house in houses: | |
print('House :') | |
print(house) | |
print("------------") | |
for house in houses: | |
if house == 'Lannister': | |
print("A Lannister always pays his debts.") | |
elif house == "Stark": | |
print("The winters are hard but the Starks will endure. We always have.") | |
else: | |
print('House ' + house) | |
########################################## | |
# 6. Dictionaries | |
########################################## | |
daenerys = { | |
'Title': 'Lady of Dragonstone, Protector of the Seven Kingdoms, The Unburnt', | |
'Origin': 'Free Cities, Dragonstone', | |
'Allegiance': 'House Targaryen', | |
'Culture': 'Valyrian', | |
'Parents': 'Aerys II, Rhaella Targaryen', | |
'Spouse': 'Khal Drogo' | |
} | |
print(daenerys['Title']) | |
daenerys['Dragons'] = 2 | |
print(daenerys['Dragons']) | |
daenerys['Dragons'] = 'Drogon & Rhaegal' | |
print(daenerys['Dragons']) | |
daenerys['Dragons'] = ['Drogon', 'Rhaegal'] | |
print(daenerys['Dragons'][0]) | |
for key, value in daenerys.items(): | |
print(value) | |
########################################## | |
# 7. Functions | |
########################################## | |
def quotes(): | |
print("Winter is coming.") | |
print("When you play a game of thrones you win or you die.") | |
quotes() | |
def multiply_by_2(number): | |
print(int(number) * 2) | |
multiply_by_2(199) | |
number = input("Give me a number: ") | |
multiply_by_2(number) | |
def get_first(list): | |
return list[0] | |
dragons = ['Drogon', 'Rhaegal', 'Viserion'] | |
first_dragon = get_first(dragons) | |
print(first_dragon) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment