python-I-primitive


variable have always a datatype, a specific kind of variable. There are two big families: primitive and collections. In this chapter we will discuss about the first.

There are 3 major primitive data types:

You can find some exercices here to practice. For strings: 🦗⬇️⬇️⬇️ Download the start gh script file here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the end gh script file here ⬇️⬇️⬇️🦗 For integer and floats: 🦗⬇️⬇️⬇️ Download the start gh script file here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the end gh script file here ⬇️⬇️⬇️🦗 And for booleans: 🦗⬇️⬇️⬇️ Download the start gh script file here ⬇️⬇️⬇️🦗 🦗⬇️⬇️⬇️ Download the end gh script file here ⬇️⬇️⬇️🦗


Char and strings

A char is a single display unit equivalent to one alphabetic symbol or a single digit, e.g. ‘m’.

On the other hand a string is a sequence of chars, e.g. “timber”

This means that each char has its position in a string and can be retrived by accessing its index (its numerical position in the string in this case).

# Strings are sequence of chars
my_var = "Hello World!"
letter = my_var[i]
print(letter)

You can manipulate string in multiple ways using indexing methods. Here’s just some of the most current.

# You can access multiple chars at once
my_var = "Hello World!"
letters = my_var[:i]
print(letters)

It is possible also to obtain the length (the number of chars) in a string by calling the function A len().

# You can access the string length
# with the method len()
my_var = "Hello World!"
length = len(my_var)
print(length)

Strings can also be seperated by a char.

# You can split strings on a character
my_var = "Hello World!"
split_var = my_var.split(' ')
print(split_var)

And they can be merged together into one new string variable.

# You can sum strings
a = "Hello"
b = " "
c = "World!"
print(a+b+c)


Intgers and Floats

An integer is a whole number that can be positive, negative or zero like e.g., 384

A float is a decimal number that can be positive, negative, or zero.

# Integers and floats (more precise)
var_int = 12
var_float = 12.0001

Floats and integers can be use with math operators.

# Addition and substraction
print(0.5 + 0.3)
print(10 - 3)

# Division
print(4/3)

# Multiplication and exponent
print(5 * 5)
print(5 ** 5)

# Modulus
print(9 % 3) # = 0
print(9 % 4) # = 1


Booleans

And finally booleans, it is a variable that can have one of two possible values, either 0 (False), or 1 (True).

Booleans are very useful to test various conditions to determine if it is True or False.

# true or false
is_true = True
is_false = False
print(is_true, is_false)

# bool operators
sum = 0.5 + 1.0
print(sum == 1.5) # equal
print(sum != 1.5) # not equal
print(sum < 1.5) # minor
print(sum >= 1.5) # major or equal

Booleans are used in a very useful synthax that you can use to adapt your script to different conditions with a if statement. Note the indent (the space on the left) that is needed to make the code readable by the machine.

# This is an "if..else" statement
if CONDITION:
····do something
else:
····do something else

# in an example
if x >= y:
····print("The value is bigger or equal to 6")
else:
····print("The value is smaller than 6")


Let’s have a look at the other big variable datatype family: collections.