Python
Python
print(“hello world”)
print(“hello, ” + [variable])
example:
name = input(“Name: “)
print (“hello, ” + name) [+ is used for concatenation]
print(f”hello, {name}”) [the curly brackets specify the value of a variable. I’m telling the
string to substitute the value of the variable]
So It would be
name = input(“Name: ”)
print(f“hello, {name}”)
CONDITIONS:
n = input(“Number: ”)
if n > 0:
print(“n is positive”)
elif n < 0: [it’s like else if. It’s another check before the else statement]
print(“n is negative”)
else:
print(“n is zero”)
This gives us a problem. I can’t compare n and 0 because n is a str and 0 is a number, so I need to
modify the input in order to convert the variable into a int. That’s why I’ve got to modify it as
follows:
n = int(input(“Number: ”))
SEQUENCES
# Hashtag is used for adding comments. The First part is for defining the list of names
names = [“Harry”, “Ron”, “Seba”]
print(names[0]) #the number between the square brackets is used for defining which element from
the list you want to print.
3.-set (collection of unique values. You don’t care about the order)
len function = Gives you the length of a saquence (number of items inside a list, number of
characters inside a string, number of elements inside a set )
LOOPING
for i in [0, 1, 2, 3, 4, 5]
print(i)
-What happens here is that we have a list and we’re telling python that for every element in the list,
call it i then print it (i is just the convention)
for i in range(6):
print(i)
range is a built in function which gives you a range of whatever amount is specified inside the
parenthesis
Loops can loop over any type of list
-------------------------------------------------------
name = “”Harry”
DICTiONARIES
Data structure to store values, similar to an actual dictionary. We look for a key word and it shows a
particular value joined to that key
houses = {“Seba”: “calle 99”, “Draco”: “Peru”} #We specify a Key calling to a value
------------------------------------------------------------------------------------------------------------
print(houses[“Pedro”])
-------------------------------------------------------------------------------------------------------------
FUNCTIONS
in order to define a function you have to use (we are gonna create a function that squares any input
number):
def square(x): #the name of the function is square and it takes an input which in this case is called
x. If there are multiple inputs they can be separated by coma. Ex. (x, y, z)
(the function can have any logic indented beneath it)
def square(x):
return x * x
for I in range(10):
print(f“the square of {I} is {square(I)}”)
CALLING THE FUNCTION FROM ANOTHER FILE
-you define the function in one individual file (module), then you can import it to another file:
-NameError: name ‘[the name of the variable]’ is not defined. This error happens when you are
calling a function from another file which you haven’t defined. That’s because you need to import
any given function that you’d like to use inside a file:
from functions import square #functions is the name of the file (don’t add extention) import square
(state here the name of the defined function inside the file mentioned before )
for I in range(10):
print(“the square of {I} is {square(I)}”)
-----------------------------------------------------------------------------------------------
Another way for doing the same:
import functions
for I in range(10):
print(“the square of {I} is {functions.square(I)}”) #in this case you need to tell it to go to
functions then inside that module go to the square function, thus functions.square
----------------------------------------------------------------------------------------------
We think of it as objects that can store data inside of them and solo support the ability to perform
some types of operations
***Class: Classes are blueprints for creating objects. They have determined characteristics that then
we are going to incorporate in as many objects as we want
class Point(): #Point can be any name you might want to give to the class
def __init__(self, x, y) : #__init__ is a reseved method (function)in python classes.
It is called as a constructor in object oriented terminology. This method is called when an
object is created from a class and it allows the class to initialize the attributes of the class.
#Self should always be included because is the way for self referring to the object so it
can store the values in itself. Always use it.
#x, y can be any value we want to give.
self.x = x
self.y = y
# here we state that the value of each point is refering to what’s stored inside the object
itself (the = something part is referring to the values we stated in (self, x, y) they are
properties)
p = Point(2, 8) #I’m calling the class that I created in the previous part
print(p.x)
print(p.y)
#in (p.x) and (p.y) we are telling the program to access to the variable p (which contains
the class inside of it) and show what the properties x and y are.
----------------------------------------------------------------------------------------------------------------
Class Flight():