Python Basics 2 — Variables and Types
This is a part of a python tutorial series based around medical examples to teach healthcare professionals how to code in Python.
A basic concept in programming is variables and their type. In some languages you have to explicitly declare a variables type, this means that variable can only be of that type, and not change. This has some advantages, but can effect the speed of development and overcomplicate learning a language, especially at this stage.
Fortunately, Python is not one of these languages. You’ve already seen one type and used it in your programme. This is a string. This is a simple type. There are other simple types; Numbers (integers/floating point) and Booleans.
Strings
Strings are effectively characters. They are declared using a single or double quotation mark. It doesn’t matter which one you use, although, using double quotations will allow you to use apostrophes. This is because if you used single quotation marks, and apostrophe is the same character and therefore would end the string early. Storing a string in a variable is as simple as follows:
myString = “This is my string” ORmyString = 'This is my string'
A single equals sign (=) is an assignment operator. The variable, myString, is assigned the value ‘This is my string’.
Numbers
There are two types of numbers that we are going to discuss here, integers and floating point numbers.
Integer
An integer is just a number, without a decimal component. An integer can be as long as you want in python, up until your computer runs out of memory.
An integer is defined as follows.
myInt = 111
Floating point numbers
A floating point number is a number including a decimal point. Floats are often used when a greater degree of accuracy is needed.
A floating point number can be declared in the following 2 ways, the second way is known as casting:
myInt = 12.1ORmyInt = float(12)
Booleans
Booleans are a very simple type. You can think of them as binary, true or false. They have many use cases, and are often used to decide if a condition is met or not. Assignment is very easy.
condition1 = trueOR condition2 = false
Operators
Simple operators can be performed on variables. Operators are symbols that tell Python to perform a mathematical task.
Strings
To combine two strings you can do the following.
string1 = "Hello"
string2 = "World"
combinedStr = string1 + ' ' + string2 + '!'print(combinedStr)
This will print to the console.
Hello World!
Numbers
You can use any operator with integers and floating points as below.
num1 = 80
num2 = 20# Addition
addition = num1 + num2# Subtraction
subtraction = num1 - num2# Multiplication
multiply = num1 * num2# Divide
division = num1 / num2print(addition, subtraction, multiply, division)
This will print to the console the following. Try it for yourself.
100 60 1600 4.0
NOTE: # in python declares a comment, this just means on that line any text following the # is ignored. You can use them in your code to tell other people why you are doing what you are doing. This is good practice.
Combining Strings and Numbers
It is possible to combine numbers within a string, however, in order to do this we need to ‘cast’ the number into a string. Casting is when you convert a variable value from one type to another. You have seen this process before using float() to create a floating point number from an integer. Type the following into your console:
age = 30
name = "Jane Dow"print(name + " is " + str(age))
If you have done this correctly, this will print to the console.
Jane Dow is 30
Wrapping age in the cast str() converts the integer 30 into a string “30”. Now that these are the same type. It is possible to append the number to the string (as you are actually combining two strings). If we did not cast the variable, then the code would not run, and you would get something called a ‘type error’.
Other useful casts can be found here.
Congratulations
You have learnt basic types in Python. These are the basis of all of your programs. More complex types are built from these simple types, which we will discuss in more detail in the next tutorial.
Previous challenge solution:
Challenge
For this weeks challenge we are going to combine everything you’ve learnt so far.
You have a patient, Jane Dow, who takes 10mg of propranolol 4 times a day. Begin with the following code:
name = "Jane Dow"
drug = "propranolol"
dose = 40
frequency = 4
You should write code that prints out the following to the console:
Jane Dow takes propranolol 40 mg 4 times a day. Total dose in one day is 160mg.
Make sure you use the variables whenever necessary so that if you changed a value the phrase would print out appropriately with the new information.
The solution will be posted at the bottom of the next tutorial.
Good luck!