Lesson 1 of 4
Store values in named variables, follow Python's naming rules, and meet the basic data types: integers, floats, strings and Booleans.
About 25 minutes
type()int(), float() and str()So far you have written numbers directly into print(). Real programs need to remember values and use them several times. A variable is a name that refers to a value stored in the computer's memory. You create one with an assignment statement: the name, an equals sign, and the value.
temperature = 298print(temperature)Read temperature = 298 as "temperature is assigned the value 298", not as an equation. Python first works out the value on the right of =, then attaches the name on the left to it. After that, wherever you use the name, Python uses the value.
A variable can be given a new value at any time. The old value is replaced. This is why they are called variables: their value can vary while the program runs.
temperature = 298print(temperature)temperature = 350 # replaces the old valueprint(temperature)Because the right-hand side is worked out first, a variable can even appear on both sides. count = count + 1 means "take the current value of count, add 1, and store the result back in count".
count = 10count = count + 1print(count)Variables make calculations readable. Instead of a line full of unexplained numbers, each value has a name that says what it is. If a value changes, you only update it in one place.
The examples below use round(value, 2), a built-in function that rounds a number to 2 decimal places. You will learn more ways to format numbers in the Strings lesson.
Copper has a face-centred cubic crystal structure with a lattice constant of 3.615 Å. The volume of its cubic unit cell is the lattice constant cubed.
lattice_constant = 3.615 # ångströmsvolume = lattice_constant ** 3print("Unit cell volume:", round(volume, 2), "ų")** means to the power of, so lattice_constant ** 3 is the lattice constant cubed.
Python has a few strict rules for variable names, and some widely followed conventions that make code easier to read.
| Rule | Valid | Not valid |
|---|---|---|
| Use letters, digits and underscores only | band_gap | band-gap, band gap |
| Don't start with a digit | sample2 | 2sample |
Don't use Python keywords such as if, for, class | material_class | class |
| Names are case-sensitive | mass and Mass are two different variables |
melting_point is far clearer than mp or x.mass_g or temperature_k.You can assign several variables in one line by separating the names and values with commas. The first name gets the first value, and so on.
a, b, c = 4.05, 4.05, 4.05 # lattice parameters in Åprint(a, b, c)Every value in Python has a type, which decides what you can do with it. You can add two numbers, for example, but not a number and a word. These are the four basic types you will use constantly:
| Type | Name | Examples | Typical use |
|---|---|---|---|
int | integer | 4, -12, 2024 | Counts: atoms per cell, number of samples |
float | floating-point number | 2.70, -0.5, 6.022e23 | Measurements: density, temperature, energy |
str | string | "Cu", 'FCC' | Text: names, symbols, labels |
bool | Boolean | True, False | Yes/no facts: is it a metal? |
Python works out the type from the value, so you don't have to declare it. The built-in type() function tells you the type of any value.
atoms_per_cell = 4density = 8.96element = "Cu"is_metal = Trueprint(type(atoms_per_cell))print(type(density))print(type(element))print(type(is_metal))Division always gives a float
/ always gives a float, even when the answer is a whole number: 8 / 2 is 4.0. You will meet whole-number division in the next lesson.Very large and very small numbers are written with e, which means times ten to the power of. 6.022e23 is 6.022 × 10²³ and 1.602e-19 is 1.602 × 10⁻¹⁹. Python also uses this notation when it prints very large or very small floats.
avogadro = 6.022e23 # particles per moleelectron_charge = 1.602e-19 # coulombsprint(avogadro)print(electron_charge)Sometimes a value has the wrong type for what you want to do. Text read from a file or typed by a user is always a string, even if it looks like a number. The functions int(), float() and str() create a new value of the type you ask for. This is called type conversion or casting.
print(float("2.70")) # text to a floatprint(int("12")) # text to an integerprint(int(3.9)) # float to integer: the decimal part is droppedprint(str(4) + " atoms")int() does not round
int(3.9) gives 3, not 4: it simply removes everything after the decimal point. Use round(3.9) if you want the nearest whole number.Choose an answer and press Check answer. You can retry as often as you like.