int_number = 33
type(int_number)
int
In contrast to integer
values numbers with a decimal point are stored as float
:
float_number = 33.33
type(float_number)
float
Note: You only need to set the decimal point so that Python knows that the value of the variable is a
float
.
There is also the possibility to change the data type of a variable manually. For instance:
int(float_number)
33
type(int(float_number))
int
Note: The explicit, manual change of a data type is referred to the term of type casting. Be aware when you cast a data type into another since not every type cast is supported. Casting could also go along with information loss. According to this you should check the result of your type cast carefully.
Strings
¶Besides numbers we also get to know about chains of characters or letters. The associated data typed is called str
for strings. Every information is automatically interpreted as string if the value is put in ""
or ''
:
a_word = "soga"
a_sentence = "I enjoy reading soga"
type(a_word)
str
type(a_sentence)
str
There is also the possibility to type cast any data type into a str
by using the str()
function. As mentioned before, be aware of casting a variable into a string object. For instance:
type(str(29))
str
The print()
function is used to display the output of an variable:
print(a_sentence)
I enjoy reading soga
Or also for user given outputs:
print("This is the soga chapter about atomar data types.")
This is the soga chapter about atomar data types.
Note: The
""
are used to indicate that the output is a string. You could also use the''
instead. The only difference is that special or reserved characters in strings are not interpreted if you use the''
. We do not want to go in detail here.
the print()
function could also be used for the concatenation of objects:
firstname = "Francis Ford"
lastname = "Coppola"
print(firstname, lastname)
Francis Ford Coppola
Note: every argument of the
print()
function is separated by a space by default.
Besides the above introduced functions there are a variety of very useful str
methods. The format()
method is a nice extension for the print()
statement:
chapter = "ninth"
soga = "soga"
print("This is the {} chapter of the {} tutorial".format(chapter, soga))
This is the ninth chapter of the soga tutorial
Note: The brackets are filled in the order which is specified within the
format()
method.Note: Please remember the different syntax between a function and a method call, which is described in detail in the previous chapter.
Besides this there exist a lot of methods that manipulate the given string for you:
a_sentence = "I enjoy reading soga"
a_sentence.title()
'I Enjoy Reading Soga'
a_sentence.upper()
'I ENJOY READING SOGA'
a_sentence.lower()
'i enjoy reading soga'
a_sentence.replace("a", "--")
'I enjoy re--ding sog--'
A full and nicely formatted list of all in build str
methods is given at W3schools.
Exercise: Use the defined variables below to reproduce the given output! Combine the defined variables with the
print()
function and do the calculation with the help of operators!
name1 = "Alan"
age1 = 20
name2 = "Marie"
age2 = 19
### your solution
print(
"{} is {} and {} is {} years old. Together they are {} years old. Their average age is {}.".
format(name1, age1, name2, age2, age1 + age2, (age1 + age2) / 2)
)
Alan is 20 and Marie is 19 years old. Together they are 39 years old. Their average age is 19.5.
bool
¶Logical values are a kind of information that only consists of two contrastive values:
0
in the meaning of no or false1
in the meaning of yes or trueNote: The advantage of this kind of data is for programming beginners normally not easy to grasp. So please do not despair if the meaning of these values is not plausible yet.
Logical values are related to comparisons between variables, so called logical expressions. The result of a logical expression is either TRUE
(0) or FALSE
(1). In this understanding they are essentially important for controlling the program flow. Although this concept will be explained in the next chapters, let's have a look at an example for the use of logical values:
x = 100
y = 200
# is x lower than y?
x < y
True
# is x greater than y?
x > y
False
# is x = y?
x == y
False
Note: the
=
operator is reserved for assigning a value to a variable. If you want to compare two variables or values with one another, you have to use the==
operator!
At last, we want to introduce the the concept of indexing a specific value out of a chain of values. The only chain of values that you know to this point are str
objects. In the next chapter you will also get to know about other common data structures that combines the introduced atomic data types to create new data objects widely used for the purposes of data science and statistical modelling. To use these data structures properly you need to understand how to retrieve one or more specific values out of a chain of values. We want to explain this with the help of a str
object:
Note: the term indexing is referred to adress the position of a value in a chain whereas the term slicing relates to the extraction of values out of a chain of values.
a_sentence = "I enjoy reading soga"
len(a_sentence)
20
To slice values out of this chain of letters, we first must think about the associated positional numbers of each letter in this character chain:
As you can see the first letter in this string is associated with the place marker $0$. The second letter with the $1$ and so on.
Note: Also spaces and special characters (e.g.
(
,)
, or*
are counted at a single letter! Be aware of this for indexing purposes.Note: It is important to know that Python starts counting with $0$. That means the first character has always the associated positional number $0$ whereas the last letter in will have the position $n - 1$, in this case 19.
To access one or more specific letter in a value chain, we make use of the [<position>]
brackets. For instance, if we want to access the first letter in a string:
a_sentence[0]
'I'
The last letter is accessed by:
a_sentence[19]
'a'
We could also make use of the len()
function which returns the length of a value chain. Beware that you need to subtract 1:
a_sentence[len(a_sentence) - 1]
'a'
Or even more convenient:
a_sentence[-1]
'a'
Accordingly:
a_sentence[-2]
'g'
a_sentence[-3]
'o'
To access multiple values out of a character chain we use the :
operator within the []
brackets. To retrieve the first 8 values:
a_sentence[0:7]
'I enjoy'
The more convenient way:
a_sentence[:7]
'I enjoy'
to get the last 4 letters:
a_sentence[-4:]
'soga'
The :
operator is really powerful. For example, if you want to get every second letter, starting with the first one:
a_sentence[::2]
'Iejyraigsg'
You could also use the ::
syntax to get all letters in a flipped order:
a_sentence[::-1]
'agos gnidaer yojne I'
We want to conclude this chapter with an Exercise:
Exercise: Check if the value stored in the variable
word
is a palindrom. Make use of logical expressions as well as the index operator!
word = "kayak"
### your solution
word == word[::-1]
True
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.