lesje-python/syntax.py
2022-12-06 18:01:23 +01:00

207 lines
4.8 KiB
Python

# Lines starting with a '#' are seen as comments, you can
# write whatever you want after them, python won't care and
# just discard it as white space
# Variables
# They are the building blocks of programming and maths,
# luckily for us the difference between python variables and
# math variables is minimal so you already know what they are.
# Variables have a name and a value. The name can be whatever
# you want it to be, it is not limited to one character.
i_am_a_variable = 5
# Here we define a variable with the name "i_am_a_variable" and
# value 5
# Python looks a lot like english and math mixed.
variable_1 = 3
variable_2 = 5
variable_3 = variable_1 + variable_2
# The print function can be used to output the value of something
# to the screen, you will learn what functions are later
print(variable_3) # 8
# Types
# Every variable has a type, in the previous example that type
# was "integer" (shortened to "int") because we were using whole
# numbers
# If we were to assign a string of text to a variable like so
stringy_string = "hello"
# Now the type of the variable "stringy_string" is a string
# (shortened to "str")
# We can request the type of something with the type function
print(type(stringy_string)) # <class 'str'>
# For floating point values it is a float
floaty_float = 3.14
print(type(floaty_float)) # <class 'float'>
some_boolean_value = False
print(type(some_boolean_value)) # <class 'bool'>
# Lists
# Lists are essentially yet another type.
# Their value is simply a list of values
listy_list = [1, 2, 3]
# Here we create a variable with name listy_list of type list
# that contains the values 1, 2, 3
# You can manipulate these lists to your liking
# For example you can insert a value like so
listy_list.append(4)
print(listy_list) # [1, 2, 3, 4]
# Or change a certain value at a given index in the list
# note that indexes start at 0.
listy_list[2] = 5
print(listy_list) # [1, 2, 5, 4]
# Tuples are very similar in that they can store a list
# of values. The big difference is that they cannot be
# manipulated
t = (1, 2, 3)
# t.append(4) would give an error
# t[2] = 5 would give an error
# Functions
def foo(x, y):
return x + y
z = foo(1, 2)
print(z) # 3
# variable_3 is now equal to 8
# As you can see the foo function is called in the same way
# we use "print". This is because print is a function too!
# Functions most of the time have a return value, this means
# that we can even chain our print and our foo function
# inside each other. This way we don't need the "z" variable.
print(foo(1, 3)) # 3
# Conditionals
# When you want to do something only IF a certain
# predicate is true, you can do that with the "if"
# keyword, surprising right
bar = 8
if bar == 8:
print("bar is equal to 8")
else:
print("bar is equal to something other than 8")
# Other comparators are
bar != 8 # bar not equal to 8
bar < 8 # bar smaller than 8
bar > 8 # bar larger than 8
bar <= 8 # bar smaller or equal to 8
bar >= 8 # bar larger or equal to 8
# and / or / not operations
# You can even chain these comparators
baz = 1
bar != 8 and baz < 4
bar < 1 or baz > 1
not bar < 1 # same as bar >= 1
# Loops
# Loops use the same conditionals as we just used above
# There are two types of loops. For loops and while loops.
# For loops are used to loop for each element of something
# (eg. a list).
# While loops are used to loop while a conditional is true
x = 0
while x < 4:
print(x)
x = x + 1
# This loop will loop 4 types and print the value of x each
# time. This means the output will look like:
# 0
# 1
# 2
# 3
sentence = ["hello", "world", "it", "is", "cold", "outside"]
for word in sentence:
print(word)
# hello
# world
# it
# is
# cold
# outside
# Classes
# Classes can be seen as a bundle of functions and variables.
# They can contain both! They can be used to structure your code.
# A class can be defined with the class keyword
class MyCoolClass:
pass # This "pass" keyword just means do nothing, ignore
# As you can see classes have a name too
# As I said they can contain functions and code as so
class CoolClass:
cool_var = 3
# This self parameter has to be added to all members functions
# of a class (unless they are static, but you can ignore that for now)
# This self reference makes it possible to change values inside the
# instance of the class that called this function.
def change_cool_var(self, new_cool_var):
self.cool_var = new_cool_var
# Now to use these classes you need to create an instance of this
# class. This is easy, just act as if it is a function call
my_class = CoolClass()
# Now the variable my_class contains an instance of CoolClass
# Now you can use the functions in this class
print(my_class.cool_var) # 3
my_class.change_cool_var(5)
print(my_class.cool_var) # 5