Data Types and Operators

They are essential to make use of the [almost] full power of variables

Data Types

What are they and why would I care?

When you define a variable you choose its type by changing it's form , each data type has different properties for different cases. They are important since you can't use only 1 data type for everything.

Ok , but how you classify them?

The data types are the following string, int, float, boolean . Now I'll say a really short summary of what each of them is and used for

INT - comes from integer and it represent any number [they need to converted to strings to be used by print | Example : variable = 42

STRING - they are a 'string' of characters . This data type is used to write any word or sentence | Example : variable = "word"

When creating a string it can be both " " and ' ' , the only difference is you can do something like

sentence = 'John said "Oh hi fellas" ' | if we used double quotes both times we would get an error , if we used only single quotes we will get an error . So if we start with single quotes we have to end with single quotes and we can't use single quotes inside other single quotes. [It's a bit hard to follow , if you didn't get it try to do

sentence = 'John said 'Oh hi fellas' ' | and you will get an error ]

FLOAT - they are like integers but they always have a dot and they are used to express numbers more accurately than INT | Example : variable = 42.23 Boolean - they can have only 2 values True or False | Example : part_4_of_tutorial = True And you will probably use these when you make state of a thing like a door [it can be either opened either closed]

When defining a variable it can be any name [if it ain't a special python function] and the underline can be used to combine several words into 1 name for a variable.

Rules of Defining a Variable with a Specific Data Type [Recap]

If you need to define a string use double quotes/single quotes . If you need to define an int , don't use double quotes and use just the numbers themselves . If you need to define a float , just make an int with a dot in it and at least 1 number after that . If you need to define a boolean , you need to give it the value True/False and without single/double quotes

A visual example to make your life easier

If you ask what's that the grayed out text that's not being run by python? Well glad you asked , those lines are commented out and you can comment out a line by using a # . In the next lesson we will talk about them in more detail

Operators

What are they used for?

Operators are obviously used to calculate stuff. It might not be useful when the input is always the same and predefined but its really useful when the input is always different.

A list of operators

Here's a list of the existing operators and what they do:

These are the basics operations

Theres's also % [which is called modulus] and it checks if the number on the right can divide the number on the left. It will return True if it can , and return False if it can't

Of course those 4 operators aren't all , you can also use exponent if you use ** . Here's some examples:

After you calculate the result you can save it in a variable . | Example : variable = 10+5 and the variable will be 15 not 10+5 [If you want it to be 10+5 , use a string]

There's also another operator and that's the equal operator = . We are already using it when we define variables and we use it the following way : <variable> = <value/variable> . The thing on the left will get the value from the thing on the right.

You can also use the operators on strings : you can make addition with them , you can multiply them and even divide them . I won't give you examples , because that will be a part of your homework for this lesson.

Another type of operators

We are now gonna talk about the comparison operators and they are the following :

These operators are really useful when making loops , but we haven't got that far . Still , there's a thing you should know about how they work :

If you do print(3>4) , what do you think it will happen? I advice you do it yourself so I don't destroy the surprise . Done? Alright so as you can see the output is False , because usually 3>4 , actually 3 is never bigger than 4. This works the same to the other ones.

I hear you ask [ why theres equal operator here too, didn't we cover it already? ] Well it's not the same equal operator , this one is translated like this "Is this number from the left the equivalent of the number on the right?" and it outputs a True/False based on what are you trying to compare.

Practice MAKES Perfect!

Your homework is of course 1. try to multiply , divide , add and subtract strings. | and 2. try to add a float with an int and see what happens , also try to make a variable that is made out of addition , subtraction , multiplaction, exponent and division at the same time and try to see whats the order of operators executing .

We are gonna discuss the answers of the homework in the next lesson

Last updated

Was this helpful?