= 60.3 weight_kg
Coding in Python
This page modifies content from the Software Carpentry Python Fundamentals lesson.
In this tutorial, you will learn the fundamentals of Python programming. We’ll cover variables, data types, and how to use built-in functions to perform basic operations.
Learning Objectives
By the end of this tutorial, you will be able to:
- Assign values to variables
- Understand basic data types in Python
- Use built-in Python functions
- Get help while programming
Variables
A variable is a name for a value. In Python, variables are created when you assign a value to them using the =
sign.
In this example, we’ve created a variable called weight_kg
and assigned it the value 60.3
.
Once a variable has been created, we can use it in calculations:
print(weight_kg)
60.3
We can also do arithmetic with variables:
print('weight in pounds:', 2.2 * weight_kg)
weight in pounds: 132.66
Variable Names
Variable names can contain letters, digits, and underscores. However, they:
- Cannot start with a digit
- Are case-sensitive
# Valid variable names
= 'inflam_001'
patient_id = 75.0
patient_weight = 45
patient1_age
# Display the variables
print('Patient ID:', patient_id)
print('Patient weight:', patient_weight, 'kg')
print('Patient age:', patient1_age, 'years')
Patient ID: inflam_001
Patient weight: 75.0 kg
Patient age: 45 years
Changing Variables
We can change the value of a variable by assigning it a new value:
= 60.3
weight_kg print('weight in kilograms:', weight_kg)
= 65.0
weight_kg print('weight in kilograms is now:', weight_kg)
weight in kilograms: 60.3
weight in kilograms is now: 65.0
Notice that when we assign a new value to a variable, it does not change other variables that might have used the old value:
= 60.3
weight_kg = 2.2 * weight_kg
weight_lb print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)
= 65.0
weight_kg print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
weight in kilograms: 60.3 and in pounds: 132.66
weight in kilograms is now: 65.0 and weight in pounds is still: 132.66
Data Types
Every value in Python has a specific type. Three common types are:
- Integer numbers (whole numbers)
- Floating point numbers (numbers with decimals)
- Strings (text)
There are many other data types, eg. bool
, list
, set
, which you will discover as you progress.
# Examples of different data types
= 25 # integer
patient_age = 68.5 # float
patient_weight = 'Alice' # string
patient_name
print('Patient name:', patient_name)
print('Patient age:', patient_age)
print('Patient weight:', patient_weight)
Patient name: Alice
Patient age: 25
Patient weight: 68.5
Finding the Type of a Variable
We can use the built-in function type()
to find out what type a value has:
print(type(60.3))
print(type('hello world'))
print(type(25))
<class 'float'>
<class 'str'>
<class 'int'>
Working with Strings
Strings must be enclosed in quotes (either single or double):
= 'inflam_001'
patient_id = "John Doe"
patient_name
print('Patient ID:', patient_id)
print('Patient name:', patient_name)
Patient ID: inflam_001
Patient name: John Doe
We can add strings together (concatenation):
= 'inflam_'
prefix = '001'
patient_number = prefix + patient_number
patient_id print('Full patient ID:', patient_id)
Full patient ID: inflam_001
Built-in Functions
Python provides many built-in functions that perform common operations. We’ve already seen print()
and type()
. (Parentheses distinguish functions from variables.)
The print() Function
The print()
function displays values:
print('Hello, world!')
print('Patient weight:', 68.5, 'kg')
Hello, world!
Patient weight: 68.5 kg
You can print multiple values by separating them with commas:
= 68.5
weight_kg = 2.2 * weight_kg
weight_lb print('Weight:', weight_kg, 'kg =', weight_lb, 'lbs')
Weight: 68.5 kg = 150.70000000000002 lbs
Mathematical Operations
Python supports common mathematical operations:
# Basic arithmetic
= 10
a = 3
b
print('Addition:', a + b)
print('Subtraction:', a - b)
print('Multiplication:', a * b)
print('Division:', a / b)
print('Integer division:', a // b)
print('Remainder:', a % b)
print('Power:', a ** b)
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Integer division: 3
Remainder: 1
Power: 1000
Getting Help
When you need help with Python, there are several ways to get information:
Using the help() Function
The help()
function provides documentation about Python functions:
help(print)
This will display detailed information about the print()
function, including its parameters and usage examples.
Other Ways to Get Help
- Online documentation: The official Python documentation at python.org
- Stack Overflow: A community-driven Q&A site where you can search for answers or ask questions
- Colleagues and mentors: Don’t hesitate to ask experienced programmers for help
- LLMs: Modern generative AI tools like ChatGPT, Claude, and GitHub Copilot can be useful to explain code. (Note that AI tools are not perfect and may make mistakes, but they will usually get fundamental questions right.)
Practice Exercises
Let’s practice what we’ve learned with some exercises:
Try to write the code by yourself before revealing the solution
Exercise 1: Variable Assignment
Create variables for a patient’s information and display them: - name: Sarah Johnson - age: 34 - height (cm): 165
Show the code
= 'Sarah Johnson'
patient_name = 34
patient_age = 165.0 # in cm
patient_height
print('Patient:', patient_name)
print('Age:', patient_age, 'years')
print('Height:', patient_height, 'cm')
Exercise 2: Data Type Investigation
What are the types of the following values?
- 3.25 - 3 - “3.25”
Use the type()
function to check:
Show the code
print('Type of 3.25:', type(3.25)) # Expect float
print('Type of 3:', type(3)) # Expect int
print('Type of "3.25":', type('3.25')) # Expect str
Exercise 3: Calculations
A patient’s body mass index (BMI) is calculated as weight (kg) divided by height (m) squared. Calculate a patient’s BMI for the following values:
- weight_kg = 70.0
- height_cm = 175.0
Show the code
# Patient data
= 70.0
weight_kg = 175.0
height_cm
# Convert height to meters
= height_cm / 100
height_m
# Calculate BMI
= weight_kg / (height_m ** 2)
bmi
print('Patient weight:', weight_kg, 'kg')
print('Patient height:', height_m, 'm')
print('Patient BMI:', round(bmi, 1))
The expected outcome is 22.9
Key Points
- Use variables to store values and make calculations
- Use
print(...)
to display values - Variables persist between cells
- Variables must be created before they are used
- Variables can be used in calculations
- Use
type(...)
to determine the type of a value - Python is case-sensitive
- Use
help(...)
to get help about functions
What’s Next?
Now that you understand Python fundamentals, you can explore:
- Working with lists and data structures
- Control flow with loops and conditionals
- Functions and modules
- Working with data using libraries like pandas
Continue practising these basics as they form the foundation for all Python programming!