Created
September 30, 2018 21:16
-
-
Save clausd/cf800253c953c976e61deb2cd67c050a to your computer and use it in GitHub Desktop.
Python basics
This file contains 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 hold values the user enters | |
# we also use them to guide program flow | |
# by the way - these are comments. They start with # - after that the rest of the line is ignored | |
number = 1 # define a variable | |
string = "Some text" # a string is a variable containing text | |
list = [1,2,3,4] # we can also define lists of things | |
number = 2 # changing the value of a variable | |
number = number + 1 # we can use the variable itself, any expression, to set the new value. The value of number is now 3 | |
number = int("3") # turning a string into a number | |
string = "The number is " + str(number) # turning a number into a string. The string is now "The number is 3" | |
number = list[0] # we can look up values in the list. The first value has index 0. The value of number is now 1 | |
number = list[3] # this is the last value in the list. The value of number is now 4 | |
# strings can be added | |
"This is now " + " a much longer string" | |
# useful functions | |
int(astring) # returns a number - if astring looks like a number. So "3", "34345" but not "three" or "Firstname Lastname". | |
str(anumber) # turns 3 into "3" and so on | |
print(anyvalue) # automatically convers anyvalue to string and prints it on the screen. | |
# program flow | |
# Using if statements we can do certain things only in certain cases | |
# In the following statement, we only print "The number is 1" | |
# when number has the value 1. Note the double == - that does not assign a value to number - but checks the value | |
if number == 1: | |
print("The number is 1") #notice the indentation. In python indentation is important | |
print("I don't know what the number is") #this is not part of the if-statement, because it's not indented | |
# we can do a little more with if | |
if number == 1: | |
print("The number is 1") | |
else: | |
print("The number is not 1") | |
# We can keep doing something until a condition becomes False | |
# This bit of program prints | |
# 1 | |
# 2 | |
# .. up to 9 | |
number = 1 | |
while number < 10: | |
print(number) | |
number = number + 1 | |
# Defining our own functions | |
# int, str and so on are nice - and there are hundreds of other functions | |
# but to build large programs we build our own functions that lets us do more complicated things | |
def afunction(aninput, anotherinput): | |
avalue = aninput + anotherinput | |
return avalue # without return, the function has no output. That's totally ok - but might not be what you expect | |
thesum = afunction(2,3) #thesum is now 5 | |
# You can put functions in other files | |
# in amodule.py | |
def anotherfunction(): | |
print("Imported this from amodule") | |
## and then here | |
import amodule | |
amodule.anotherfunction() | |
# | |
# OR | |
from amodule import anotherfunction | |
anotherfunction() | |
## Handling errors | |
try: | |
int("Not a number") | |
except: | |
print("That was not a number!") | |
int("Not a number") # the program stops, because we didn't contain the error | |
## saving ond loading things | |
# saving a number to a file. | |
with open('filename', 'w') as f: | |
f.write(str(number)) | |
# reading it again | |
with open('filename') as f: | |
number = int(f.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment