Skip to content

Commit

Permalink
Uploaded Beginner Code Snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyMSchafer committed May 17, 2017
1 parent 950c294 commit 2165e57
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Python-Conditionals/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

condition = 'Test'

if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
24 changes: 24 additions & 0 deletions Python-Conditionals/snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Comparisons:
# Equal: ==
# Not Equal: !=
# Greater Than: >
# Less Than: <
# Greater or Equal: >=
# Less or Equal: <=
# Object Identity: is


# False Values:
# False
# None
# Zero of any numeric type
# Any empty sequence. For example, '', (), [].
# Any empty mapping. For example, {}.

condition = False

if condition:
print('Evaluated to True')
else:
print('Evaluated to False')
5 changes: 5 additions & 0 deletions Python-Dicts/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CompSci']}

for key, value in student.items():
print(key, value)
25 changes: 25 additions & 0 deletions Python-Functions/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Number of days per month. First value placeholder for indexing purposes.
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def is_leap(year):
"""Return True for leap years, False for non-leap years."""

return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


def days_in_month(year, month):
"""Return number of days in that month in that year."""

# year 2017
# month 2
if not 1 <= month <= 12:
return 'Invalid Month'

if month == 2 and is_leap(year):
return 29

return month_days[month]

print(days_in_month(2017, 2))
21 changes: 21 additions & 0 deletions Python-Functions/snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Number of days per month. First value placeholder for indexing purposes.
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def is_leap(year):
"""Return True for leap years, False for non-leap years."""

return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


def days_in_month(year, month):
"""Return number of days in that month in that year."""

if not 1 <= month <= 12:
return 'Invalid Month'

if month == 2 and is_leap(year):
return 29

return month_days[month]
2 changes: 2 additions & 0 deletions Python-Imports/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

courses = ['History', 'Math', 'Physics', 'CompSci']
13 changes: 13 additions & 0 deletions Python-Imports/my_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

print('Imported my_module...')

test = 'Test String'


def find_index(to_search, target):
'''Find the index of a value in a sequence'''
for i, value in enumerate(to_search):
if value == target:
return i

return -1
8 changes: 8 additions & 0 deletions Python-Ints/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

num_1 = '100'
num_2 = '200'

num_1 = int(num_1)
num_2 = int(num_2)

print(num_1 + num_2)
18 changes: 18 additions & 0 deletions Python-Ints/snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# Arithmetic Operators:
# Addition: 3 + 2
# Subtraction: 3 - 2
# Multiplication: 3 * 2
# Division: 3 / 2
# Floor Division: 3 // 2
# Exponent: 3 ** 2
# Modulus: 3 % 2


# Comparisons:
# Equal: 3 == 2
# Not Equal: 3 != 2
# Greater Than: 3 > 2
# Less Than: 3 < 2
# Greater or Equal: 3 >= 2
# Less or Equal: 3 <= 2
12 changes: 12 additions & 0 deletions Python-Lists/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Empty Lists
empty_list = []
empty_list = list()

# Empty Tuples
empty_tuple = ()
empty_tuple = tuple()

# Empty Sets
empty_set = {} # This isn't right! It's a dict
empty_set = set()
44 changes: 44 additions & 0 deletions Python-Lists/snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


# Mutable
list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1

print(list_1)
print(list_2)

# list_1[0] = 'Art'

# print(list_1)
# print(list_2)


# Immutable
# tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
# tuple_2 = tuple_1

# print(tuple_1)
# print(tuple_2)

# tuple_1[0] = 'Art'

# print(tuple_1)
# print(tuple_2)

# Sets
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}

print(cs_courses)


# Empty Lists
empty_list = []
empty_list = list()

# Empty Tuples
empty_tuple = ()
empty_tuple = tuple()

# Empty Sets
empty_set = {} # This isn't right! It's a dict
empty_set = set()
8 changes: 8 additions & 0 deletions Python-Loops/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

x = 0

while True:
# if x == 5:
# break
print(x)
x += 1
5 changes: 5 additions & 0 deletions Python-Strings/intro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

greeting = 'Hello'
name = 'Michael'

print(help(str.lower))

0 comments on commit 2165e57

Please sign in to comment.