Skip to content

Commit 328f756

Browse files
Create Young Pythonistas.py
1 parent af7d793 commit 328f756

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed

Young Pythonistas.py

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
# This is for much younger Pythonistas to be. lol
2+
3+
# With computers, numbers always follow the order of operation
4+
# or BEDMAS: 'Brackets, Exponents, Division, Addition and
5+
# Subtraction.
6+
7+
# Here are some Python examples that clearly show the order
8+
# of operation.
9+
10+
print(3*4+5+3) # 12+8 = 20
11+
12+
num1=3*4
13+
num2=5+3
14+
15+
print(num1+num2) # 12+8 = 20
16+
print(num1-num2) # 12-8 = 4
17+
18+
# To create an integer on the screen output instead of a float,
19+
# use the int() function. For example: 20.0 is a float, 20 is an
20+
# integer number without a decimal point. In some cases, a
21+
# floating point number can occur when you don't want to
22+
# display an ugly floating point number on the screen output.
23+
24+
print(int(24/2+5+3)) # 12+8 = 20
25+
26+
num1=int(24/2)
27+
num2=int(5+3)
28+
29+
print(num1+num2) # 12+8 = 20
30+
print(num1-num2) # 12-8 = 4
31+
'''----------------------------------------------------------------------------------------------------------------------'''
32+
# Add the sum of all the numbers in a numbers list.
33+
34+
numbers=0,1,2,3,4,5,6,7,8,9
35+
36+
print(sum(numbers)) # sum = 45
37+
'''----------------------------------------------------------------------------------------------------------------------'''
38+
# Minimum and maximum number example:
39+
40+
min_max=0,1,2,3,4,5,6,7,8,9
41+
42+
print(min(min_max)) # min = 0
43+
print(max(min_max)) # max = 9
44+
45+
# To keep things simpler. Why not do this:
46+
47+
min_num=min(0,1,2,3,4,5,6,7,8,9)
48+
max_num=max(0,1,2,3,4,5,6,7,8,9)
49+
50+
print(min_num)
51+
print(max_num)
52+
'''----------------------------------------------------------------------------------------------------------------------'''
53+
# Round numbers with the 'round()' function
54+
55+
round_up=round(85.5) # round = 86
56+
57+
print(round_up)
58+
59+
round_up=round(85.56,1) # round = 85.6
60+
61+
print(round_up)
62+
'''----------------------------------------------------------------------------------------------------------------------'''
63+
# The 'abs()' function returns a negative integer into a
64+
# positive number.
65+
66+
positive_num=abs(-3)
67+
68+
print(positive_num) # abs = 3
69+
'''----------------------------------------------------------------------------------------------------------------------'''
70+
# Float function example:
71+
72+
float_num=float(4) # float = 4.0
73+
74+
print(float_num)
75+
'''----------------------------------------------------------------------------------------------------------------------'''
76+
# Floor division example:
77+
78+
import math
79+
80+
floor_num=math.floor(8.5) # floor = 8
81+
82+
print(floor_num)
83+
'''----------------------------------------------------------------------------------------------------------------------'''
84+
# Ceil division example:
85+
86+
import math
87+
88+
ceil_num=math.ceil(8.5) # ceil = 9
89+
90+
print(ceil_num)
91+
'''----------------------------------------------------------------------------------------------------------------------'''
92+
# Modulus division example:
93+
94+
import math
95+
96+
mod_num=5%3 # modulus '%' = 2 as the remainder
97+
98+
print(mod_num)
99+
'''----------------------------------------------------------------------------------------------------------------------'''
100+
# Try and Except Error Handler Examples:
101+
102+
# The basic layout of the Try and except error handler.
103+
104+
try:
105+
pass # empty placeholder for code, until needed.
106+
except ValueError:
107+
pass
108+
finally: # optional: executes no matter the outcome.
109+
pass
110+
111+
# This is a working example of the Try and except error handler.
112+
113+
try:
114+
message=int(input('Please enter a number: ').strip())
115+
except ValueError: # catches non integer values
116+
print('Error! Numbers only please.')
117+
finally: # optional: executes no matter the outcome.
118+
print('finally: always executes no matter the outcome.')
119+
'''----------------------------------------------------------------------------------------------------------------------'''
120+
# Dictionary, nested for-loop sentence example:
121+
122+
# Create three dictionaries called person_name, pet_name
123+
# and pet.
124+
125+
person_name={1:'Rob',2:'Bob',3:'Tom',4:'John'}
126+
pet_name={1:'Spot',2:'Sylvester',3:'Goergy',4:'Polly'}
127+
pet={1:'Dog',2:'Cat',3:'Rat',4:'Bird'}
128+
129+
# Create three lists called person_name_list, pet_name_list
130+
# pet_list to get all the dictionary values.
131+
132+
person_name_list=[
133+
person_name.get(1),person_name.get(2),
134+
person_name.get(3),person_name.get(4)]
135+
136+
pet_name_list=[
137+
pet_name.get(1),pet_name.get(2),
138+
pet_name.get(3),pet_name.get(4)]
139+
140+
pet_list=[
141+
pet.get(1),pet.get(2),
142+
pet.get(3),pet.get(4)]
143+
144+
# Create a his_her variable to add more programming
145+
# creativity within the nested for-loop.
146+
147+
his_her=['his','her']
148+
149+
# Use the (f') format() function to combine strings together,
150+
# without the worry about proper string concatenation.
151+
152+
for i in range(2):
153+
for j in range(4):
154+
print(f'My name is {person_name_list[j]}. \
155+
I love my {pet_list[j]} and {his_her[i]} name is {pet_name_list[j]}.')
156+
157+
# You can still use of the depreciated old format() function:
158+
159+
for i in range(2):
160+
for j in range(4):
161+
print('My name is {}. I love my {} and {} name is {}.\
162+
'.format(person_name_list[j],pet_list[j],his_her[i],pet_name_list[j]))
163+
'''----------------------------------------------------------------------------------------------------------------------'''
164+
# Create a set that will check and get rid of duplicated
165+
# letters. Note: sets don't do indexing; you must 'cast'
166+
# the set into a list first with the list() function.
167+
168+
alphaset={
169+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','k',
170+
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r','v',
171+
's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a','a'}
172+
173+
# Convert the set into a list, while sorted without actually
174+
# sorting the real alphalist values.
175+
176+
alphalist=list(sorted(alphaset)) # cast with the list() function
177+
178+
# Create a for-loop to loop through the alphalist values,
179+
# while calling the upper() function to turn the letters
180+
# into uppercase letters.
181+
182+
for i in alphalist:
183+
print(i.upper(),end=' ') # end=' ' forces same line printout
184+
185+
# Use the len() function to count how many letters there
186+
# are inside the alphalist variable. Insert a new line with '\n'.
187+
188+
print('\n',len(alphalist))
189+
190+
# or this:
191+
192+
# Use the str() function to concatenate strings with a '+' sign.
193+
194+
print('\n'+str(len(alphalist)))
195+
196+
# have some fun, while calling the lower() function to turn
197+
# the uppercase sentence into a lowercase sentence.
198+
199+
print('\n'+str(len(alphalist)),'LETTERS IN THE ALPHABET.'.lower())
200+
201+
# Create a variable for the sentence instead.
202+
203+
sentence='LETTERS IN THE ALPHABET.'
204+
205+
print('\n'+str(len(alphalist)),sentence.lower())
206+
207+
'''Python 3 and up:'''
208+
209+
# Use the (f') format() function to combine strings together,
210+
# without the worry about proper string concatenation.
211+
212+
print(f'\n{len(alphalist)} {sentence}'.lower())
213+
214+
'''Python 1, and 2 versions'''
215+
216+
# You can still use of the depreciated old format() function:
217+
218+
print('\n{} {}'.format(len(alphalist),sentence.lower()))
219+
220+
# Call the alphalist with a slice index [:]
221+
222+
print(alphalist[0:26])
223+
print(alphalist[5:26])
224+
225+
# Print a single letter from the alphalist variable.
226+
227+
print(alphalist[0].upper())
228+
229+
# Create a for-loop to print out the whole alphabet by using
230+
# the variable 'alphalist' instead of using a range(n,n)
231+
232+
for i in alphalist:
233+
print(i.upper(),end=' ')
234+
235+
# Print the whole alphabet using a slice index [:] within
236+
# a for-loop.
237+
238+
for i in range(26):
239+
print(alphalist[i].upper(),end=' ')
240+
241+
# Print the whole alphabet backwards using a slice
242+
# index [:] within a reverse for-loop.
243+
244+
for i in range(25,-1,-1):
245+
print(alphalist[i].upper(),end=' ')
246+
247+
# Show off your Python skills with some fancy footwork to
248+
# keep things straight. Make the three for-loop examples
249+
# above, read like these three examples below.
250+
251+
for i in alphalist:print(i.upper(),end=' ')
252+
253+
for i in range(26):print(alphalist[i].upper(),end=' ')
254+
255+
for i in range(25,-1,-1):print(alphalist[i].upper(),end=' ')
256+
'''----------------------------------------------------------------------------------------------------------------------'''
257+
# How to shrink down lines of code, using a simicolon ' ; '
258+
# along with the colon ' : '
259+
260+
# Please note: some Python code won't allow the use of
261+
# the simicolon ' ; ' to be able to completely shrink down
262+
# Python code. In for-loops and while loops, the colon ' : '
263+
# cannot be used for nested loops or beside lists.
264+
# This simple Python code is as shrunk down as it can
265+
# physically allow.
266+
267+
for i in range(1,11):print(i)
268+
269+
x=[0,1,2,3,4,5,6,7,8,9]
270+
for i in x:print(i)
271+
272+
x=1
273+
while x<=10:print(x);x+=1
274+
275+
x=3
276+
while True:
277+
if x==4:print(x,'Breaks out of the loop.');break
278+
elif x==5:print('Continue as we loop forever!');continue
279+
else:print('else: and break broke you out of the loop anyway.');break
280+
'''----------------------------------------------------------------------------------------------------------------------'''
281+
# Shorten functions down examples:
282+
283+
def func1():print('Function 1')
284+
def func2():print('Function 2')
285+
def func3():print('Function 3')
286+
287+
# Call each function from the funcs list with a for-loop.
288+
289+
funcs=[func1,func2,func3]
290+
291+
for i in funcs:i()
292+
'''----------------------------------------------------------------------------------------------------------------------'''

0 commit comments

Comments
 (0)