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.

Dr. Matt, MBBS BSc
Doctors in Tech
4 min readJul 2, 2019

--

Photo by Shahadat Shemul on Unsplash

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:

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.

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:

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.

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.

This will print to the console.

Numbers

You can use any operator with integers and floating points as below.

This will print to the console the following. Try it for yourself.

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:

If you have done this correctly, this will print to the console.

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:

You should write code that prints out the following to the console:

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!

Follow us on Twitter and Facebook.

--

--

Dr. Matt, MBBS BSc
Doctors in Tech

Medical Doctor | Medical Technology | Neurology | Published Researcher | While I have your attention, you may as well scroll down.