Python Operations#
Python is a great platform to do basic math operations. Printing out the information is quick and simple, and you don’t need to worry about any weird syntax. Some poeple even prefer using python instead of a general calcualtor app.
There are 2 main ways to use Python, either via a terminal command window, or via a program file. Both offer pros and cons.
In a terminal, you can type python3 and you should see >>> pop up. This will mean that you are now in a python interface. To verify, you can type “print("hello!"),” and press enter, your command window should respond by printing hello!. Your terminal window is a great place to test out quick operations or anything that you want to try before modifiying a current file you may be working on. You can store vairables temprarirly inside of your terminal window and whatnot, but it is not reccomended to do anything too complex that you may try to replicate at some point.
If you are doing more than running a few lines of instructions, it is reccomended you write your code in a file instead. This will allow you to re-run all of your code and modify it as needed.
If you ever want to print anything out in your command line so you can see what is happening, the print function to do so. print("hello") will print hello in the command line. Once you have variables and whatnot, you can print those too by just doing print(x) or whatever variable you may be using.
Below are some print statements showing how different operations work inside of python. Feel free to change values, and add more lines to try out.
Note: Using # in your code will allow you to make comments. This is really important to document your code, and explain what is going on. Anything that is after a # on a line is not counted as code, and will not be executed. # can also be used to ‘comment out’ different parts of code you don’t want to run right away.
print(2+4) #addition
print(2-4) #subtraction
print(2*4) #multiplication
print(2**4) #exponential
print(2/4) #division
print(2//4) #floor division (rounds down)
print(2%4) #modular division (remainder from the division)
print(2**4*(1+2)) #parentheses and order of opperations are important!
print(2**4*1+2)
6
-2
8
16
0.5
0
2
48
18
Boolean Operations#
There are times where using a simple boolean operation is the fastest way to make a system work. A boolean is a type of data that is simply a 1 or a 0, True or False, high or low, on or off. We use booleans in real life too, we just call it true or false usually though, but anything that has only 2 states, such as a general light switch, is technically a boolean operator.
Whenever we are asking a ‘yes’ or ‘no’ question in programming, such as if something is equal, or greater or less than something, we are asking for a boolean response. In our flow charts, will ask a yes or no question, and then do something based on the response. We can do one thing if the statement is True, and we can do something different if the statement is False.
print(1>3) #greater than
print(1<3) #less than
print(1>=3) #greater than or equal to
print(1<=3) #less than or equal to
print(1==3) #equal to (in most programming languages, if we are asking if something is equal, we need to use == instead of =)
print(1!=3) #not equal
False
True
False
True
False
True
We also have access to not which will output or expect the opposite of whatever is prormpted. (not True is False and not False is True)
print(not 1>3)
print(not 1<3)
print(not 1==3)
True
False
True
Note! Booleans in Python have the first letter CAPITALIZED. true does not work in Python, make sure to use True!
We can also ask multiple things at once using and and or operators. Check out the boolean page for more information.
#Sandbox Area