On Github mbrochh / python-101
Press SPACE or SHIFT+SPACE to move through the slides...
Big thanks to Red Hat & PayPal Singapore for kindly hosting this event and for sponsoring pizza and softdrinks.
PyLadiesSG has a community on Slack. Myself and others are idling there permanently and we are happy to mentor anyone who needs help.
Visit pyladies-sg-slackin.herokuapp.com if you are based in Singapore and if you would like an invite.
python birthday.py 1982-09-08 10
I will turn 1 on Thursday, 1983-09-08 I will turn 2 on Saturday, 1984-09-08 I will turn 3 on Sunday, 1985-09-08 I will turn 4 on Monday, 1986-09-08 I will turn 5 on Tuesday, 1987-09-08 I will turn 6 on Thursday, 1988-09-08 I will turn 7 on Friday, 1989-09-08 I will turn 8 on Saturday, 1990-09-08 I will turn 9 on Sunday, 1991-09-08 I will turn 10 on Tuesday, 1992-09-08
print("Hello World")
variable_name = 1
message = "Hello World!"
is_simple = True
something = None
BIRTHDAY = "1982-09-08" print(BIRTHDAY)
variable_name = "Your string"
my_string = "Hello World!" my_string = my_string.upper() print(my_string)
> HELLO WORLD!
my_age = 33
my_string = "I am {} years old".format(my_age)
print(my_string)
> I am 33 years old
BIRTHDAY = "1982-09-08"
message = "I am born on {}".format(BIRTHDAY)
print(message)
python birthday.py
python birthday.py 1982-09-08
import datetime now = datetime.datetime.now() print(now)
> 2015-12-08 02:19:15.529046
import sys
BIRTHDAY = sys.argv[1]
message = "I am born on {}".format(BIRTHDAY)
print(message)
python birthday.py 2015-01-01
if 1 > 2:
print("One is bigger than two")
# This code is actually never reached
else:
print("One is not bigger than two")
> One is not bigger than two
import sys
if len(sys.argv) < 2:
print("ERROR: Please provide a date")
else:
BIRTHDAY = sys.argv[1]
message = "I am born on {}".format(BIRTHDAY)
print(message)
import datetime my_date = datetime.datetime(2015, 12, 08) print(my_date)
2015-12-08 00:00:00
import datetime my_string = "2015-12-08" my_date = datetime.datetime.strptime(my_string, "%Y-%m-%d") print(my_date)
2015-12-08 00:00:00
import datetime my_date = datetime.datetime(2015, 9, 8) print(my_date.isoweekday())
2
import sys
from datetime import datetime
if len(sys.argv) < 2:
print("ERROR: Please provide a date")
else:
BIRTHDAY = datetime.strptime(sys.argv[1], "%Y-%m-%d")
message = "I am born on {}".format(BIRTHDAY.date())
print(message)
value1 = 1
value2 = 0
try:
result = value1 / value2
except ZeroDivisionError:
print("ERROR: You can't divide by zero")
> ERROR: You can't divide by zero
import sys
from datetime import datetime
if len(sys.argv) < 2:
print("ERROR: Please provide a date")
else:
try:
BIRTHDAY = datetime.strptime(sys.argv[1], "%Y-%m-%d")
except ValueError:
print("ERROR: Please enter a correct date")
sys.exit()
message = "I am born on {}".format(BIRTHDAY.date())
print(message)
def function_name(arg1, arg2, kwarg1=None, kwarg2=None): result = arg1 + arg2 return result
result = function_name(1, 2, kwarg1="Hello"): print(result)
import sys
from datetime import datetime, date
def get_future_date(base_date, years):
future_date = date(
base_date.year + years,
base_date.month,
base_date.day
)
return future_date
if len(sys.argv) < 3:
print("ERROR: Please provide a date and an age")
else:
try:
birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
except ValueError:
print("ERROR: Please enter a correct date")
sys.exit()
years = int(sys.argv[2])
future_date = get_future_date(birthday, years)
message = "I will turn {} on {}".format(years, future_date)
print(message)
my_variable = []
my_variable = [1, 2, 3]
my_variable.append(4) my_variable.remove(4)
my_variable[1] # This should return `2`, because lists are zero-indexed
numbers = [1, 2, 3] more_numbers = [4, 5, 6] all_numbers = numbers + more_numbers print(all_numbers) all_numbers.reverse() print(all_numbers) all_numbers.append(7) print(all_numbers) all_numbers.sort() print(all_numbers)
[1, 2, 3, 4, 5, 6] [6, 5, 4, 3, 2, 1] [6, 5, 4, 3, 2, 1, 7] [1, 2, 3, 4, 5, 6, 7]
my_list = range(100) my_other_list = range(1, 100, 1) print(my_list) print(my_other_list)
var my_list = [1, 2, 3, 4]
for my_number in my_list:
if my_number == 1:
continue
print (my_number)
import sys
from datetime import datetime, date
def get_future_date(base_date, years):
future_date = date(
base_date.year + years,
base_date.month,
base_date.day
)
return future_date
if len(sys.argv) < 3:
print("ERROR: Please provide a date and an age")
else:
try:
birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
except ValueError:
print("ERROR: Please enter a correct date")
sys.exit()
years = int(sys.argv[2])
years_list = range(1, years + 1, 1)
for year in years_list:
future_date = get_future_date(birthday, year)
message = "I will turn {} on {}".format(year, future_date)
print(message)
my_dict = {}
my_dict = {
"Martin": 33,
"Eva": 24,
}
martins_age = my_dict["Martin"]
import sys
from datetime import datetime, date
def get_weekday_name(weekday_number):
weekdays = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday",
}
return weekdays[weekday_number]
def get_future_date(base_date, years):
future_date = date(
base_date.year + years,
base_date.month,
base_date.day
)
return future_date
if len(sys.argv) < 3:
print("ERROR: Please provide a date and an age")
else:
try:
birthday = datetime.strptime(sys.argv[1], "%Y-%m-%d")
except ValueError:
print("ERROR: Please enter a correct date")
sys.exit()
years = int(sys.argv[2])
years_list = range(1, years + 1, 1)
for year in years_list:
future_date = get_future_date(birthday, year)
weekday = get_weekday_name(future_date.isoweekday())
message = "I will turn {} on {}, {}".format(year, weekday, future_date)
print(message)