Note
This module has been adapted from https://carpentries.org/, which is freely available for reuse under the creative commons licence.
What basic data types can I work with in Python?
How can I create a new variable in Python?
How do I use a function?
Can I change the value associated with a variable after I create it?
How can I get help while learning to program?
Any Python interpreter can be used as a calculator:
This is great but not very interesting.
To do anything useful with data, we need to assign its value to a variable. In Python, we can assign a value to a variable, using the equals sign =. For example, we can track the weight of a patient who weighs 60 kilograms by assigning the value 60 to a variable weight_kg:
From now on, whenever we use weight_kg, Python will substitute the value we assigned to it. In laypersonâs terms, a variable is a name for a value.
variable names:
This means that, for example:
weight0 is a valid variable name, whereas 0weight is notweight and Weight are different variablesPython knows various types of data. Three common ones are:
In the example above, variable weight_kg has an integer value of 60.
If we want to more precisely track the weight of our patient, we can use a floating point value by executing:
To create a string, we add single or double quotes around some text. To identify and track a patient throughout our study, we can assign each person a unique identifier by storing it in a string:
Once we have data stored with variable names, we can make use of it in calculations. We may want to store our patientâs weight in pounds as well as kilograms:
We might decide to add a prefix to our patient identifier:
To carry out common tasks with data and variables in Python, the language provides us with several built-in functions. To display information to the screen, we use the print function:
When we want to make use of a function, referred to as calling the function, we follow its name by parentheses. The parentheses are important: if you leave them off, the function doesnât actually run! Sometimes you will include values or variables inside the parentheses for the function to use. In the case of print, we use the parentheses to tell the function what value we want to display. We will learn more about how functions work and how to create our own in later episodes.
We can display multiple things at once using only one print call:
We can also call a function inside of another function call. For example, Python has a built-in function called type that tells you a valueâs data type:
Moreover, we can do arithmetic with variables right inside the print function:
Note: The above command, however, did not change the value of weight_kg:
To change the value of the weight_kg variable, we have to assign weight_kg a new value using the equals = sign:
Use the built-in function help to get help for a function. Every built-in function has extensive documentation that can also be found online.
This help message (the functionâs âdocstringâ) includes a usage statement, a list of parameters accepted by the function, and their default values if they have them.
It is normal to encounter error messages while programming, whether you are learning for the first time or have been programming for many years. We will discuss error messages in more detail later. For now, letâs explore how people use them to get more help when they are stuck with their Python code.
Search the internet: paste the last line of your error message or the word âpythonâ and a short description of what you want to do into your favourite search engine and you will usually find several examples where other people have encountered the same problem and came looking for help.
StackOverflow can be particularly helpful for this: answers to questions are presented as a ranked thread ordered according to how useful other users found them to be.
Take care: copying and pasting code written by somebody else is risky unless you understand exactly what it is doing!
Ask somebody âin the real worldâ.
If you have a colleague or friend with more expertise in Python than you have, show them the problem you are having and ask them for help.
We will discuss more debugging strategies in greater depth later in the lesson.
A variable in Python is analogous to a sticky note with a name written on it: assigning a value to a variable is like putting that sticky note on a particular value.
Using this analogy, we can investigate how assigning a value to one variable does not change values of other, seemingly related, variables. For example, letâs store the subjectâs weight in pounds in its own variable:
Everything in a line of code following the â#â symbol is a comment that is ignored by Python. Comments allow programmers to leave explanatory notes for other programmers or their future selves.
Similar to above, the expression 2.2 * weight_kg is evaluated to 143.0, and then this value is assigned to the variable weight_lb (i.e. the sticky note weight_lb is placed on 143.0). At this point, each variable is âstuckâ to completely distinct and unrelated values.
Letâs now change weight_kg:
Since weight_lb doesnât ârememberâ where its value comes from, it is not updated when we change weight_kg.
variable = value to assign a value to a variable in order to record it in memory.print(something) to display the value of something.# some kind of explanation to add comments to programs.help(thing) to view help for something.Data Science and Digital Skills