At the end of this activity, you will be able to:
- Be able to create, modify and use objects or variables in
- Be able to define the key differences between the str (string) and num (number) classes in
Pythonin terms of how
pythoncan or can not perform calculations with each.
What You Need
Python 3.x and
Jupyter notebooks to complete this tutorial. Also you should have an
earth-analytics directory setup on your computer with a
/data directory with it.
You can get output from
Python by typing a mathematical equation into the console - For example, if you type in
3 + 5,
Python will calculate the output value.
# add 3 + 5 3 + 5
# divide 12 by 7 12 / 7
However, is it more useful to assign values to objects. To create an object, you need to give it a name followed by the assignment operator
=, and the value you want to give it:
# assign weight_kg to the value of 55 weight_kg = 55 # view object value weight_kg
Expressive readable object names
You can name our objects in python anything that you want. For example:
subject_id. However, it is best to use clear, descriptive words when naming objects to ensure your code is easy to follow. Using a naming convention that explains to someone reading the code what the object is or in the case of a function, what it does, is often referenced as as element of Expressive Programming.
You wil learn best practicing for coding a bit later - in the clean coding lesson. For now, here are some tips to improve your code:
- Keep object names short: this makes them easier to read when scanning through code.
- Use meaningful / expressive names that describe the contents of the object that you are creating: For example:
precipis a more useful name that tells us something about the object compared to
- Don’t start names with numbers! Objects that start with a number are NOT VALID in
- Avoid names that are existing functions in
for, see here
A few other notes about object names in
Pythonis case sensitive (e.g.,
weight_kgis different from
- Avoid other function names (e.g.,
- Use nouns for variable names, and verbs for function names.
- Avoid using dots in object names - e.g.
my.dataset- dots have a special meaning in
Python(for methods) and other programming languages. Instead use underscores
View object value
When assigning a value to an object,
Python does not print anything. You can force it to print the value by using parentheses or by typing the name:
# here weight_kg is assigned to the value 55 however nothing is printed weight_kg=55 # a variable name at the end of a cell will be printed by Jupyter notebook weight_kg
# python is case sensitive Weight_kg
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-b8c9261fc186> in <module>() ----> 1 Weight_kg NameError: name 'Weight_kg' is not defined
Python has stored
weight_kg in memory, you can do arithmetic with it. For instance, you may want to convert this weight in pounds (weight in pounds is 2.2 times the weight in kg):
2.2 * weight_kg
You can also change a variable’s value by assigning it a new value:
weight_kg=57.6 2.2 * weight_kg
Assigning a value to one variable does not change the values of other variables. For example, let’s store the animal’s weight in pounds in a new variable,
weight_lb = 2.2 * weight_kg
and then change
weight_kg to 100.
What do you think is the current content of the object
weight_lb? 126.5 or 200?
Optional challenge activity
What are the values of each object defined in EACH LINE OF code below?
mass = 47.5 # mass? age = 122 # age? mass = mass * 2.0 # mass? age = age - 20 # age? mass_index = mass / age # mass_index?
Check your answers by running the code in python!