Skip to content

Instantly share code, notes, and snippets.

@Almenon
Created October 15, 2019 04:33
Show Gist options
  • Save Almenon/e550cebe47911d41e1f459edbc3c7da5 to your computer and use it in GitHub Desktop.
Save Almenon/e550cebe47911d41e1f459edbc3c7da5 to your computer and use it in GitHub Desktop.
how to program, the basics
from arepl_dump import dump
#$end
# The first part of programming is simply declaring your variables
# This is very similar to math
x = 1
my_cool_number = 1+1
# my cool number = 1+1 # this doesn't work, no spaces allowed!
z = 2*2
my_list = [1,2,3]
# but you're not just limited to numbers!
aliyah = "awesome person"
dog = {
"legs": 4,
"ears": 2
}
# so now we have our data. But what to do with it?
# Programming is about giving commands to a computer
# one such command is print
# you can run commands by typing the name of the command followed by parenthesis
# For example, print()
print()
# That doesn't do anything.. because you didn't tell it what to print
# let's print aliyah from earlier!
print(aliyah)
# we got an awesome person!
# you can print practically anything
print("testing")
print(x)
print(dog)
# What if we want to print a specific number from the my_list?
# You can access lists or dictionaries with []
print(my_list[0])
# this prints the first number of the list
# *HERMOINE FRANTICALLY RAISES HER HAND*: teachER teacher you put 0!
# Well, in programming lists start at 0.
# Q: Why?
# A: Who cares? Google it.
# What about dictionaries?
# We can get the number of legs for the dog in the same form
# We just pass in a string instead of a number
print(dog["legs"])
# Moving on, you can combine things with +
print(aliyah+" with a great face")
# but you can't combine two different types
# like doritos and ice cream mixed together, computer says barf!
print(aliyah+my_cool_number)
# let's make both of them strings, now things are cool again
print(aliyah+str(my_cool_number))
# Those are the basics, we are a halfway there!
# the beauty of a computer is it allows us to be lazy
# lets say you are punished to write a number a 1000 times
# We could do:
print(1)
print(2)
print(3)
# and I'm tired already
# let's try LOOPS
for number in range(1000):
print(number)
# that was easy!
# let's break down what we did as a reminder:
# for number in range(1000):
# ^ for and in and the : at the end are special loop syntax. Like lists starting at 0, it's just a fact of life.
# ^ we ran a command called range and told it to give us a range of 1000 numbers
# ^ each number gets assigned to the variable `number`
# print(number)
# ^ we run the print command
# ^ and pass in the number!
# we can loop over my_list
for num in my_list:
print(num)
# We can even loop over the dog!:
for key in dog:
print(key)
# This won't work, get rid of the # and try to fix it
# (hint: it's missing just one thing!)
#for number in [1,2,3]
# print(number)
# But what if the teacher changes the punishment to write "foo" if the number is even?
# Do we have to write everything out?
# nope!
for number in range(1000):
if number%2==0: # %2 divides us by two and gives us the remainder.
# if it's cleanly divisible then we will get 0 and this path is taken
print("foo")
else:
# otherwise this path is taken
print(number)
# And if we have to write "buzz" if the number is divisible by 5?
for number in range(1000):
if number%2==0:
print("foo")
elif number%5 == 0: #elif stands for "else if". Sometimes programmers are too lazy to even type out a complete wor
print("buzz")
else:
# otherwise this path is taken
print(number)
# That's basic programming!
# There's a lot more to learn
# and we lied a tiny bit to simplify things
# but most of programming is just variables, commands, if's, and loops when you get down to it
# OPTIONAL READING:
"""lies:
1. not all languages start at 0. A few langauges like Matlab start at 1.
2. Not all languages use "for foo in bla:" syntax for loops. But the majority of them do have at least the word for
3. In javascript, you CAN combine different types. This is convenient but also leads to a lot of bugs
4. Classes and functions and imports and more are also big parts of programming. But not technically necessary.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment