Last active
January 18, 2020 21:57
-
-
Save m3g4p0p/1e8e15d25e8ae0026fbb1d198a63d92e to your computer and use it in GitHub Desktop.
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
# Hello. This is a commant, and as such | |
# getting ignored by the interpreter | |
# Types I -- Primitives | |
1 # This is an integer (int) | |
1.1 # This is a float (float) | |
"blah" # This is a string (str ) | |
True # This is a boolean (bool) | |
None # This is not anything (NoneType) | |
""" This is a multi line string. | |
These are also commonly used | |
for comments, and for code | |
documentation in particular | |
""" | |
# Assignment Operator | |
spam = 42 | |
eggs = 'the meaning of life' | |
""" We can now access spam and eggs, and | |
get or set their respective values | |
""" | |
ham = spam | |
# I / O | |
something = input('> ') | |
print('you entered', something) | |
# Comparison Operators | |
print( | |
spam == 42, # True | |
spam > 0, # True | |
spam == eggs, # False | |
spam != eggs # True | |
) | |
# Arithmetic Operators | |
print( | |
1 + 1, # 2 | |
spam - 1, # 41 | |
spam * spam # 1764 | |
(1 + 1) / 3 # 0.6666666666666666 | |
) | |
""" Other arithmetic operators are % (modulo), | |
** (power); for more esoteric functions | |
concerned with rounding and such please | |
have a look at the math library... | |
https://docs.python.org/3.5/library/math.html | |
""" | |
# Module Imports | |
# https://www.xkcd.com/353/ | |
import math | |
print(math.sqrt(9)) # 3 | |
from math import pi | |
print(pi) # 3 | |
from math import pi as weirdnumber | |
print(weirdnumber) # 3 | |
# Logical Operators | |
print( | |
not False, # True | |
True is False, # False | |
True is not False, # True | |
True and False, # False | |
False or True, # True | |
) | |
# Types II -- Casting | |
print( | |
type(spam) == int, # True | |
float(spam), # 42.0 | |
spam == '42', # False | |
str(spam) == '42', # True | |
spam == int('42') # True | |
) | |
# Lists | |
values = [1, 2, 3] | |
print( | |
2 in values, # True | |
4 not in values, # True | |
len(values) # 3 | |
) | |
values.append(4) | |
print(values) # [1, 2, 3, 4] | |
values.reverse() | |
print(values) # [4, 3, 2, 1] | |
last = values.pop() | |
print(last, values) # 1 [4, 3, 2] | |
values[2] = 42 | |
values[0] = values[0] + values[1] | |
print( | |
values, # [7, 3, 42] | |
values.index(42), # 2 | |
values[-1], # 42 | |
values[1:3], # [3, 42] | |
values[:2] # [7, 3] | |
) | |
# Control Flow I -- Conditionals | |
if int(something) in values: | |
print(something, 'is in', values) | |
elif something == eggs: | |
print(something, 'is not in', values, 'but it is', eggs) | |
else: | |
print(something, 'is neither in', values, 'nor is it', eggs) | |
# Control Flow II -- Loops | |
while input('> ') != str(spam): | |
print('Please try again!') | |
while True: | |
guess = input('> ') | |
if not guess: | |
continue | |
if int(guess) == spam: | |
break | |
print('Please try again!') | |
# Control Flow III -- Iteration | |
result = 0 | |
for value in values: | |
result = result + value | |
print(result == sum(values)) # True | |
# Functions | |
def double(value): | |
""" A function can (but does not have to) | |
accept parameters that are again passed | |
as arguments, and accessible within the | |
scope of the function like regular | |
variables. | |
A function may also return a value | |
that is available to the caller just | |
like any other expression | |
""" | |
return value + value | |
twenty = double(10) | |
print(twenty) # 20 | |
print(double(spam)) # 84 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment