In this lesson, we will start with the very basics of python using this iPython notebook. We can use Python in a variety of methods (at the terminal, using an IDE etc. However the notebook is a great way to play with python with the support of lots of helpful notes. Let's start of getting to know Python with using the classic program 'Hello world'
In [ ]:
print "Hello world"
To get the output of the above program, select the cell and then click the play button to run the command using Python
As you may notice in the code block above a Python keyword 'print' is highlighted in a color (green). We call 'print' a key word because this word has special meaning to Python. Python is considered one of the best languages for new coders to learn because compared to other programming languages it is much more intuitive. In the C language for example, printing 'Hello World' would look something like this:
#include< stdio.h >
main() { printf("Hello World");
}
Although learning Python (and learning it well) will take effort, you can see why its considered simpler for a new learner. However Python does have rules you will have to follow. In the following execises, let's see what some of these rules are.
In [ ]:
print "hello world"
Did you get 'Hello World' - is it capitolized correctly, is it exactly like the output of the example program?
Tip: You can reuse any of the empty code blocks. Just rewite any code and hit play again to rerun the cell.
In [ ]:
In [ ]:
In [ ]:
In [ ]:
2 + 2
You can use any of several math operators:
Syntax | Math |
---|---|
( ) | Parenthensis - think PEMDAS |
a**b | exponentiation |
a*b | multiplication |
a/b | division |
a+b | addition |
a-b | subtraction |
There are a few other special math functions - see Python basic math
Try some of the math functions below:
In [ ]:
# Do some subtraction
In [ ]:
# Use some parenthesis
In [ ]:
# Try some division
Depending on what you tried to divide, you may have gotten some troubling results, for example:
In [ ]:
10 / 3
Is there something wrong with Python?
In [ ]:
#try this...
(10 / 3) * 3 == 3 * 3
Bizzare! What it comes down to is that Python uses something called floor division. Basically, when you ask Python to divide two integers Python will round down. What about non-integers?
In [ ]:
10 / 3.0
Here you have given Python a float
: 3.0. This floating point number or float
makes it clear to Python that it should not round down, and should just give you the full demical value. Be sure to always use floats when you are doing division. There are some other ways to choose how Python does division, but we will just stick to this for now.
What makes Python a language is how it uses syntax to hold or express meaning. As mentioned before, Python let's you display messages to use user simply by using the word print
followed by a message in "
marks. Using the =
sign - which is called the assignment operator
we can ask Python to store values as variables:
In [ ]:
a = 3
b = 4
c = 5
a_squared_plus_b_squared = a**2 + b**2
c_squared = c**2
print a_squared_plus_b_squared
print c_squared
Challenge: Solve this equation showing your work using Python:
How much 5M NaCl do you need to make 1L of 2.5M NaCl?
hint Concentration1 Volume1 = Concentration2 Volume2
hint 2 solve for volume 1
In [ ]:
Besides math, variables could also be statements. When we put statements in characters, we refer to them as strings
, which can be sequences of letters, numbers, symbols, etc.
In [ ]:
my_string = 'Hello World'
print my_string
Create the variables and strings in the next codeblock to complete a mini autobiography:
In [ ]:
#Populate your variables before the print statements
print 'My name is: ',my_name
print 'My favorite food is: ',my_fav_food
print 'The hobby I spend most of my time is: ', my_hobby
print 'If I wasn\'t here, I\'d probably be: ',my_somewhere_else
print "If I wasen't using single quotes, I'd be using double quotes"
#tip, in the last print statement notice that we want to use the ' mark. We use the
# 'escape' character \ to tell Python to ignore that quote mark.
With math and strings, you can start to do lots of things in Python. More on this in the next lesson.
In [ ]:
my_string = "'ello world "
print my_string * 10
print my_string * 100
Want to try somthing else, keep going!
In [ ]: