Skip to content

Instantly share code, notes, and snippets.

@thinkallabout
Created November 4, 2016 01:03
Show Gist options
  • Save thinkallabout/a79ae4e0f32eb650aca378159592d6ec to your computer and use it in GitHub Desktop.
Save thinkallabout/a79ae4e0f32eb650aca378159592d6ec to your computer and use it in GitHub Desktop.
Quick and easy Python tutorial
### 1. BASIC STATEMENTS ###
# These are examples of basic data types in Python. They're basically the same
# in all programming languages but are all presented slightly differently.
# String: "Hello"
# Number: 5
# Boolean: False
# List: []
# Tuple: []
# Prints the computed value of x to the screen. Usually used for debugging.
print('Hello, world!')
# Prints 200
print(10*20)
# Prints True
print(True)
### 2. VARIABLES ###
# Stores the value seperately so it can be reused. This does the same as above.
hello = 'Hello, world!'
print(hello)
# Change the variable. Prints "nope".
hello = 'nope'
print(hello)
### 3. BOOLEAN ALGEBRA ###
# 1 is considered 'truthy'. Any value or object evaluates to true unless it's a
# 0, False, None or an empty array.
if 1:
print('1 is true')
# This statement doesn't evaluate to true because these are all falsy values.
if 0 or False or None:
# Pass just means 'ignore this' when you don't want anything to happen.
print('Something above is true')
# An empty array is False.
if []:
pass
# However an array with values in it is true.
if [0, 1]:
print('This list is true')
### 4. I/O ###
### 5. Data Structures ###
# Create a list of animals and print it.
animals = ['dog', 'cat', 'rabbit']
print(animals)
# Print the first animal in the list. Arrays start from 0 and not 1.
print(animals[0])
# Change the 2nd value (cat to frog).
animals[1] = 'frog'
# Add giraffe onto the end of the list.
animals.append('giraffe')
# There are more data structures such as:
# 1. Tuples.
# The same as lists but the size _has_ to stay the same.
# 2. Maps.
# These are key-value, so a key matches to a value. e.g
# customer01: "Cameroon Broon"
# customer02: "..."
### 6. Iteration ###
# A small set of instructions can be run over and over.
for x in range(0, 10):
# This will print 11 times (not 10, because 0 is the start, not 1).
# The str(x) function makes x into a string. Only strings can be added onto
# strings. Likewise int(x) makes a string (with a number inside) into a
# proper python number.
print("Times this function has been run: " + str(x))
animals = ['dog', 'cat', 'rabbit']
# Print every animal in animals. This goes through every item in a list and
# puts the value of each item into "animal". The word animal can be anything you
# want, but it makes sense to be animal here.
for animal in animals:
print(animal)
### 7. Functions ###
# Returns the value of x multiplied by y, with ten added on.
def multiply_numbers_and_add_ten(x, y):
return (x * y) + 10
# Store the value in a variable.
value = multiply_numbers_and_add_ten(5, 10)
# Prints 10
print(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment