What Is Python
Python is an easy to use high level programming language. Python is great for begginers and genneraly easy to use. It has a design philosophy emphasizing the readability of code and unlike most languages uses whitespace and indentation to delimit blocks. The language's core philosophy is summarized in the document The Zen of Python (PEP 20), which includes:
- Beautiful is better than ugly
- Explicit is better than implicit
- Simple is better than complex
- Complex is better than complicated
- Readability counts
Python Setup
Python is free and reletively easy to set up. just go to https://www.python.org/downloads/ and download the appropriate version for your opperating system. once it is downloaded run the setup and follow the instructions given to you on screen
IDLE
At this point python should installed on your computer, if it is not please go to the Python Setup Section Python is packedged with a nice easy to use graphical Integrated Development Unit (normaly abbreviated "IDE"). Named in part after Eric Idle from Montey Python, pythons standard IDE is called IDLE for "Integrated DeveLopment Environment". You can open it by going into your python folder and clicking on the application "IDLE". Once it opens up the first thing that comes up is a shell, because python is an interpereted language it doesn't need to be completed and compiled before you run it. If you have ever used your computers terminal or command line it is not unlike that, you can type in commands and it will procsess them on the fly as you type them in.
Hello World
To get started and create your first Hello World program, open up IDLE and click File > New File. Save this as hello.py where ever you want to keep your code, I suggest going into your documents and creating a folder called python-projects. The .py is the python extention this is how your computer knows it is a python script.
Type out print ("Hello World!")
into your program. Save your work. Click Run > Run Module
Your shell should have popped up with the text Hello World!
Congrats you have created your first python program.
In the hello world program the code that was used was print ("Hello World")
, print is a function that takes in what you put in the parentheses it and prints it out to the screen. You can give it a value or an expression. A value is something that is already in its final state. Say you know that you want the program to say Its cold outside
then you would call the print function with the value inside the parenthese. Make sure the value is inside of quotes if you are working with words, numbers do not need them:
print ("Its cold outside")
Its cold outside
An expression is something that needs to be solved still. If I have two numbers 5 and 10 and I want to print out what those two numbers are added together you can do that inside the parentheses of print and it will return the result.
print (5 + 10)
15
You can also add two sets of words, or strings together, this is called concatenating. Look what happens when we add the string "Have a " to the string "good day" and print them out.
print ("Have a " + "good day")
Have a good day
Note: although you can make expressions as long as you want its always a good idea to keep your code readable and clear. Your statements should be concice enough to where someone who looks at your code should be able to tell what you are trying to accoplish with every line. When printing out print ("Have a " + "nice day")
there are only two things being added, which is pretty easy to understand. Something like print ("H" + "i" + " " + "h" + "o" + "w" + " "a" + "r" + "e" + "y" + "o" + "u" + " " + "t" + "o" + "d" + "a" + "y" + "?")
may functionally work, but it would be better practice to combine the string in advance and then send it like so print ("Hi how are you today?")
.
Variables
In computing there are a few main abilities that allow you to solve complicated issues. Those main abilities are Variables: the ability to store and recall information, Comparison: the ability to compare values and expressions and do different actions based on different results, and Iteration: the ability to run the same peice of code multiple times. Variables are the simplist concept out of the three. Just like in math you can assign value to a letter or word and then use that value later on when you need it. When naming variables you can use capital A-Z lowercase a-z and numbers 1-10. You cannot start with a number and for now it is reccomneded that you start off every variable with lowercase letters, although you can start them off with capital letters this usually denotes they are a type of data that you wont learn about for a little while. The notation for storing a value in a variable is variable_name = value/expression
. So lets say I want to store three numbers and then add them together and store the result too: num1, num2, num3, and num4, and then print out their values on the screen
num1 = 1
num2 = 5
num3 = 10
num4 = num1 + num2 + num3
print (num1)
print (num2)
print (num3)
print (num4)
1
5
10
16
Variables can be changed on the go, you can rewrite over a variable with new data whenever you want. So continuing on from the code above we will add 1 to num3 and see what all of the values are
num3 = num3 + 1
print (num1)
print (num2)
print (num3)
print (num4)
1
5
11
16
As you can see we did not touch variables num1 or num2 so they are the same value as they were before. num3 is now 11, it was 10. How did this work? Well when you assign a value to a variable it evaluates from right to left. the statement num3 = num3 + 1
starts at the right side and evaluates any expressions first, the expression is num3 + 1
to evaluate that the program needs finds out what the value of num3 is, so it checks in its memory and pulls out the number 10, now it knows both numbers 10 and 1 and adds them together to get the number 11. The expression is now evaluated and the result is 11, next the computer checks what you want to do with it, there is an equals sign so it knows you want to store the information. It looks on the left side of the equals sign, num3 is there so it stores the value 11 in num3.
What about the last value of num4, it still has a value of 16 but it was made of num1 + num2 + num3 so shouldn't it be 17 now? No because it is no longer ascociated with those numbers. When num4 was created it went through the same process that we just went throgh for num3. it evaluated the expression by finding the values of num1, num2, and num3, which were 1, 5, and 10 respectivly, it added them up and the result was 16. It then saw the number 16 was going to be stored in a variable and put it in storage associated with the variable num4. Once this action was complete it had no connection to any of the parts of the expression it was set to. That is we told it to set itself to the values that the variables represented, and not to the variables themselfs.
Here is another example, say we have a bunch of characters that we want to print together. We can concatenate them all together and store them in a variable. We can do that like so:
phrase = "H" + "i" + " " + "h" + "o" + "w" + " "a" + "r" + "e" + "y" + "o" + "u" + " " + "t" + "o" + "d" + "a" + "y" + "?"
print (phrase)
Hi how are you today?
Operators
An operator is a shortcut for a function in python that has widespread or essential use. E.g. the addition sign is an opperator in python, the commonly used opperator a + b
is shortended from operator.add (a, b)
Math Operators
Operation | Syntax | Function |
---|---|---|
Addition | a + b | operator.add(a, b) |
Subtraction | a - b | operator.sub(a, b) |
Multiplication | a * b | operator.mul(a, b) |
Division | a / b | operator.truediv(a, b) |
Exponentiation | a ** b | operator.pow(a, b) |
Modulo | a % b | operator.mod(a, b) |
Inplace Operators
Inplace Operators are shortcuts for changing variables inplace. E.g. var = a
you want to add b to var, you can do var += b
instead of var = var + b
Operation | Equivalent | Syntax | Function |
---|---|---|---|
Add in place | a = a + b | a += b | operator.iadd(a, b) |
Subtract in place | a = a - b | a -= b | operator.isub(a, b) |
Multiply in place | a = a * b | a *= b | operator.imul(a, b) |
Divide in place | a = a / b | a /= b | operator.itruediv(a, b) |
Exponentiation in place | a = a ** b | a **= b | operator.ipow(a, b) |
Modulo in place | a = a % b | a %= b | operator.imod(a, b) |
Comparison Operators
Comparison operators make it easy to evaluate a statement, most commonly used for if statements. E.g. if (a > b): a = b
Operation | Syntax | Function |
---|---|---|
Equality | a == b | operator.eq(a, b) |
Non equality | a != b | operator.ne(a, b) |
Greater than | a > b | operator.gt(a, b) |
Greater than or equal to | a >= b | operator.ge(a, b) |
Less than | a < b | operator.lt(a, b) |
Less than or equal to | a <= b | operator.le(a, b) |
If Statements
Conditional statements are essential to creating programs that are more than just a static output. In python the main way of control flow is through loops and through conditional statements called if statements.
a = 6
b = 5
if (a > b): print ("a is bigger than b")
if (b > a): print ("b is bigger than a")
a is bigger than b
The expression given to the if statement is evaluated, if it is true than the code immediately following it will be executed. If it is false it will look for an elif or else statement. An elif is short for else if, as in if the if statement is false it will check to see if the conditional statement in the elif is true, if it is true it will execute the code following it, if it is false it will check the next elif statement, if there are no more elif statements it will see if there is an else statement and execute the code following that.
a = 5
b = 10
if (a > b): print ("a is bigger than b")
elif (b > a): print ("b is bigger than a")
else: print ("a is the same size as b")
b is bigger than a
a = 5
b = 5
if (a > b): print ("a is bigger than b")
elif (b > a): print ("b is bigger than a")
else: print ("a is the same size as b")
a is the same size as b