Lesson 3: Python Basics 1 - Variables#
This lesson is modified from A taste of Python from geo-python.
Objectives#
By the end of this lesson, you will be able to
Define and update python variables
Apply these concepts to write simple python code
1. Variables#
1.1 Define a variable#
A variable
can be used to store values calculated in expressions and used for other calculations.
To assign a value, you simply type variable_name = value
In the cell below, define a variable called temp_celsius
, assign it a value of ‘10.0’.
Then print that variable value using the print()
function.
Note that you should do this on two separate lines.
temp_celsius=10.0
print(temp_celsius)
10.0
Print out the value of temp_celsius
in degrees Fahrenheit.
Note: tempFahrenheit = (temp_celsius * 9/5) + 32
The output should read ‘Temperature in Fahrenheit: 50.0’.
tempFahrenheit = (temp_celsius * 9/5) + 32
print('Temperature in Fahrenheit:',tempFahrenheit)
Temperature in Fahrenheit: 50.0
Check your understanding#
Define any two variables that you like with text or numeric values.
Print the two variables using print
function
Show code cell content
name='Bibo'
age=8
print('my name is', name, 'and my age is', age)
my name is Bibo and my age is 8
1.2 Updating variables#
Values stored in variables can also be updated. Let’s redefine the value of temp_celsius
to be equal to 15.0 and print its value in the cells below.
temp_celsius=15.0
print(temp_celsius)
15.0
Warning
If you try to run some code that accesses a variable that has not yet been defined you will get a NameError
message. Try printing out the value of the variable TempFahrenheit
using the print()
function in the cell below.
print(TempFahrenheit)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 1
----> 1 print(TempFahrenheit)
NameError: name 'TempFahrenheit' is not defined
Define the variable:
TempFahrenheit = 9 / 5 * temp_celsius + 32
Now try again
TempFahrenheit = 9 / 5 * temp_celsius + 32
print(TempFahrenheit)
59.0
Check your understanding#
Write a code the converts temperature in celsius to Fahrenheit and prints to screen the temperature in both celsius and fahrenheit. Here is the an example of the output “20 degrees celsius is 68 degree Fahrenheit”
temp_celsius=20.0
tempFahrenheit= 9 / 5 * temp_celsius + 32
print(temp_celsius, 'degrees celsius is', tempFahrenheit,'degree Fahrenheit')
20.0 degrees celsius is 68.0 degree Fahrenheit
2. Data types#
A data type
determines the characteristics of data in a program.
There are 4 basic data types in Python as shown in the table below.
Data type name |
Data type |
Example |
---|---|---|
|
Whole integer values |
|
|
Decimal values |
|
|
Character strings |
|
|
True/false values |
|
The data type can be found using the type()
function.
As you will see, the data types are important because some are not compatible with one another.
Let’s define a variable weatherForecast
and assign it the value 'Hot'
. After this, we can check its data type using the type()
function.
weatherForecast='Hot'
type(weatherForecast)
str
Let’s also check the type of tempFahrenheit
. What happens if you try to combine tempFahrenheit
and weatherForecast
in a single math equation such as tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast
?
type(tempFahrenheit)
float
tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[18], line 1
----> 1 tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast
TypeError: can't multiply sequence by non-int of type 'float'
In this case we get at TypeError
because we are trying to execute a math operation with data types that are not compatible. There is no way in Python to multpily decimal numbers with a character string.
Check your understanding#
As it turns out, you can do some math with character strings in Python. Define two variables and assign them character string values in the Python cell below.
Show code cell content
# Here is an example solution
var1='Hi'
var2= 'Bye'
What happens if you try to add two character strings together print(first_variable + second_variable)
?
print(var1+var2)
HiBye
Can we subtract them print(first_variable - second_variable)
?
print(var1-var2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 print(var1-var2)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Which other math operations work for character strings? Check multiplication 5*first_variable
print(5*var1)
HiHiHiHiHi
3. Character input (optional)#
Python and Jupyter notebooks also let us interact in another way with our code! The built-in input() function reads a line from input and returns it as a string.
Let’s try for example
place = input("Where are you from? ")
print(place, "is a nice place!")
Cairo is a nice place!
Warning
Jupyter Notebooks might sometimes get stuck when using the input()
function. If this happens, restart the kernel.
Let’s try another example in the cell below using the similar approach. Ask the user for a temperature in Celsius using the input()
function and print the input value to the screen.
tempCelsius = input("How cold can it be in Fort Myers (in degrees Celsius)? ")
print(tempCelsius, "degrees Celsius is quite cold!")
10 degrees Celsius is quite cold!
What is the data type of variable place
? What about the other variable you defined? Check their data types using the cell below.
print(type(place))
print(type(temp_celsius))
<class 'str'>
<class 'float'>
What happens when you try to convert your Celsius temperature value to Fahrenheit using the equation from earlier in the lesson?
tempFahrenheit = 9 / 5 * temp_celsius + 32
How to solve this problem?
Check online how to convert str to float.
tempCelsius=float(temp_celsius)
tempFahrenheit = 9 / 5 * temp_celsius + 32
print(tempFahrenheit)
68.0