Last active
October 15, 2017 15:16
-
-
Save evaldosantos/d4e5d40234c896af7fd61eb98d5f709a 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
# variables | |
name = "Evaldo" | |
age = 27 | |
# output | |
print(name) | |
print(age) | |
# strings | |
'Evaldo' | |
"Evaldo" | |
'evaldo\'s' | |
'evaldo is {}'.format(age) | |
''' | |
evaldo | |
is | |
27''' | |
""" | |
evaldo | |
is | |
27""" | |
# lists | |
# strings are lists like | |
name = "Evaldo" | |
name[1] -> returns 'v' | |
l = [1,2,3,4] | |
l[-1] -> returns 4 | |
>>> [1,2]+[3,4] | |
[1, 2, 3, 4] | |
>>> "hello"+"world" | |
'helloworld' | |
>>> "Hello"*2 | |
'HelloHello' | |
>>> [1,2]*2 | |
[1, 2, 1, 2] | |
>>> a = [1,2] | |
>>> a.append(3) | |
>>> a | |
[1, 2, 3] | |
>>> a.append([4,5]) | |
>>> a | |
[1, 2, 3, [4, 5]] | |
>>> a.remove([4,5]) | |
>>> a | |
[1, 2, 3] | |
>>> a.extend([4,5]) | |
>>> a | |
[1, 2, 3, 4, 5] | |
>>> del a[0] | |
>>> a | |
[2, 3, 4, 5] | |
# booleans | |
False | |
True | |
# everything that does't have a value or is empty is falsy | |
bool([]) -> return False | |
bool("") -> return False | |
bool(None) -> return False | |
bool([1,2,3]) -> return True | |
bool("Evaldo") -> return True | |
# boolean operations | |
== | |
!= | |
# in python we don't have 'coercion' operators, ===, !== | |
>, <, >=, <=, in | |
>>> 5 in [1,2,3,4,5] | |
True | |
>>> 6 in [1,2,3,4,5] | |
False | |
>>> "cheese" in "cheeseshop" | |
True | |
is -> tests if two objects have the same reference | |
1 is 1 | |
>>> True | |
>>> a = "Evaldo" | |
>>> b = "Evaldo" | |
>>> a is b | |
True | |
>>> [1,2] is [1,2] | |
False | |
>>> "Evaldo" is "Evaldo" | |
True | |
>>> not True | |
>>> False | |
>>> not False | |
>>> True | |
# if/else/operators | |
>>> days_open = ['Monday','Tuesday','Wednesday','Thursday'] | |
>>> today = 'Tuesday' | |
>>> if today in days_open: | |
... print("come on in!") | |
... else: | |
... print("Sorry, we're closed") | |
... | |
come on in! | |
>>> if today not in days_open: | |
... print("Sorry, we're closed") | |
... | |
Sorry, we're closed | |
# Loops | |
>>> a = [1,2,3,4] | |
>>> while a: | |
... print a[0] | |
... del a[0] | |
... | |
1 | |
2 | |
3 | |
4 | |
>>> for a in [1,2,3,4]: | |
... print a | |
... | |
1 | |
2 | |
3 | |
4 | |
>>> for a in [1,2,3,4]: | |
... if a < 3: | |
... continue | |
... print a | |
... | |
3 | |
4 | |
>>> for a in [1,2,3,4]: | |
... if a > 3: | |
... break | |
... print a | |
... | |
1 | |
2 | |
3 | |
>>> while a: | |
... print a | |
... a -= 1 | |
... else: | |
... print("finished") | |
... | |
10 | |
9 | |
8 | |
7 | |
6 | |
5 | |
4 | |
3 | |
2 | |
1 | |
finished | |
>>> for a in [1,2,3,4]: | |
... print a | |
... else: | |
... print("finished") | |
... | |
1 | |
2 | |
3 | |
4 | |
finished | |
# To use else with for/while loops the loop can't throw exceptions or have break | |
>>> for a in [1,2,3,4]: | |
... if a > 2: | |
... break | |
... print a | |
... else: | |
... print("finished") | |
... | |
1 | |
2 | |
# Input / Output | |
>>> age = input("what's your age? ") | |
what's your age? 28 | |
>>> age | |
'28' | |
>>> age + 1 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: must be str, not int | |
>>> age = int(input("what's your age? ")) | |
what's your age? 28 | |
>>> age+1 | |
29 | |
>>> print(age) | |
28 | |
# functions | |
def even(num): | |
if num % 2 == 0: | |
return True | |
else: | |
return False | |
# Exceptions (the exception to the rule) | |
try: | |
speed = int(input("What is the airspeed velocity of an unladen swallow? ")) | |
except ValueError: | |
print("What? I don't kno-oooooooooooooooo!") | |
else: | |
print("I think a swallow could go faster than {}.".format(speed)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment