Skip to content

Commit 2165e57

Browse files
committed
Uploaded Beginner Code Snippets
1 parent 950c294 commit 2165e57

File tree

13 files changed

+192
-0
lines changed

13 files changed

+192
-0
lines changed

Python-Conditionals/intro.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
condition = 'Test'
3+
4+
if condition:
5+
print('Evaluated to True')
6+
else:
7+
print('Evaluated to False')

Python-Conditionals/snippets.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Comparisons:
3+
# Equal: ==
4+
# Not Equal: !=
5+
# Greater Than: >
6+
# Less Than: <
7+
# Greater or Equal: >=
8+
# Less or Equal: <=
9+
# Object Identity: is
10+
11+
12+
# False Values:
13+
# False
14+
# None
15+
# Zero of any numeric type
16+
# Any empty sequence. For example, '', (), [].
17+
# Any empty mapping. For example, {}.
18+
19+
condition = False
20+
21+
if condition:
22+
print('Evaluated to True')
23+
else:
24+
print('Evaluated to False')

Python-Dicts/intro.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
student = {'name': 'John', 'age': 25, 'courses': ['Math', 'CompSci']}
3+
4+
for key, value in student.items():
5+
print(key, value)

Python-Functions/intro.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Number of days per month. First value placeholder for indexing purposes.
3+
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
4+
5+
6+
def is_leap(year):
7+
"""Return True for leap years, False for non-leap years."""
8+
9+
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
10+
11+
12+
def days_in_month(year, month):
13+
"""Return number of days in that month in that year."""
14+
15+
# year 2017
16+
# month 2
17+
if not 1 <= month <= 12:
18+
return 'Invalid Month'
19+
20+
if month == 2 and is_leap(year):
21+
return 29
22+
23+
return month_days[month]
24+
25+
print(days_in_month(2017, 2))

Python-Functions/snippets.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Number of days per month. First value placeholder for indexing purposes.
3+
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
4+
5+
6+
def is_leap(year):
7+
"""Return True for leap years, False for non-leap years."""
8+
9+
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
10+
11+
12+
def days_in_month(year, month):
13+
"""Return number of days in that month in that year."""
14+
15+
if not 1 <= month <= 12:
16+
return 'Invalid Month'
17+
18+
if month == 2 and is_leap(year):
19+
return 29
20+
21+
return month_days[month]

Python-Imports/intro.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
courses = ['History', 'Math', 'Physics', 'CompSci']

Python-Imports/my_module.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
print('Imported my_module...')
3+
4+
test = 'Test String'
5+
6+
7+
def find_index(to_search, target):
8+
'''Find the index of a value in a sequence'''
9+
for i, value in enumerate(to_search):
10+
if value == target:
11+
return i
12+
13+
return -1

Python-Ints/intro.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
num_1 = '100'
3+
num_2 = '200'
4+
5+
num_1 = int(num_1)
6+
num_2 = int(num_2)
7+
8+
print(num_1 + num_2)

Python-Ints/snippets.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Arithmetic Operators:
3+
# Addition: 3 + 2
4+
# Subtraction: 3 - 2
5+
# Multiplication: 3 * 2
6+
# Division: 3 / 2
7+
# Floor Division: 3 // 2
8+
# Exponent: 3 ** 2
9+
# Modulus: 3 % 2
10+
11+
12+
# Comparisons:
13+
# Equal: 3 == 2
14+
# Not Equal: 3 != 2
15+
# Greater Than: 3 > 2
16+
# Less Than: 3 < 2
17+
# Greater or Equal: 3 >= 2
18+
# Less or Equal: 3 <= 2

Python-Lists/intro.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Empty Lists
3+
empty_list = []
4+
empty_list = list()
5+
6+
# Empty Tuples
7+
empty_tuple = ()
8+
empty_tuple = tuple()
9+
10+
# Empty Sets
11+
empty_set = {} # This isn't right! It's a dict
12+
empty_set = set()

Python-Lists/snippets.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
# Mutable
4+
list_1 = ['History', 'Math', 'Physics', 'CompSci']
5+
list_2 = list_1
6+
7+
print(list_1)
8+
print(list_2)
9+
10+
# list_1[0] = 'Art'
11+
12+
# print(list_1)
13+
# print(list_2)
14+
15+
16+
# Immutable
17+
# tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
18+
# tuple_2 = tuple_1
19+
20+
# print(tuple_1)
21+
# print(tuple_2)
22+
23+
# tuple_1[0] = 'Art'
24+
25+
# print(tuple_1)
26+
# print(tuple_2)
27+
28+
# Sets
29+
cs_courses = {'History', 'Math', 'Physics', 'CompSci'}
30+
31+
print(cs_courses)
32+
33+
34+
# Empty Lists
35+
empty_list = []
36+
empty_list = list()
37+
38+
# Empty Tuples
39+
empty_tuple = ()
40+
empty_tuple = tuple()
41+
42+
# Empty Sets
43+
empty_set = {} # This isn't right! It's a dict
44+
empty_set = set()

Python-Loops/intro.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
x = 0
3+
4+
while True:
5+
# if x == 5:
6+
# break
7+
print(x)
8+
x += 1

Python-Strings/intro.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
greeting = 'Hello'
3+
name = 'Michael'
4+
5+
print(help(str.lower))

0 commit comments

Comments
 (0)