Python: Integers, Floating Point Numbers, Complex Numbers, Collections/lists and Loops


-Integers-

Within Python Integers are zero, positive or negative whole numbers without a fractional part to any of them while also having unlimited precision and length for example: 

Input:  x = 5

                y = 78614304825499175

                z = -15042031700

                print(type(x))

                print(type(y))

               print(type(z))

Output:  <class 'int'> 

                    <class 'int'> 

                    <class 'int'>

By using the command "print(type(x))" you can identify the class/type of a certain value, just like in the example show above with X, Y and Z you can see that they are all integers. 

-floating point numbers-

In Python floating point numbers are numbers that are positive or negative that contain one or more decimals for example: 

Input:  x = 4.15

                y = 7.0

                z = -55.55

               print(type(x))

               print(type(y))

               print(type(z))

Output:  <class 'float'> 

                    <class 'float'> 

                    <class 'float'>

Just like when we looked at integers, we use the exact same command to identify the class/type of these values here which show to be float (Floating Point Numbers).  Floating point numbers can also be scientific numbers with an "e" within it to indicate the power of 10, for example: 

Input: x = 36e4
               y = 15E4
               z = -97.7e100

              print(type(x))
              print(type(y))
              print(type(z))


Output:  <class 'float'>

                    <class 'float'>

                    <class 'float'>

As you can see even as scientific numbers, they can also be presented as Float (Floating Point Numbers). With floating point numbers we can indicate that a whole number value is a floating-point number by adding a decimal point after it.

-complex numbers-

Within Python Complex Numbers are typically written with a "J" as the imaginary part like the following:

Input:  x = 9+6j

                y = 3j

                z = -7j

               print(type(x))

               print(type(y))

               print(type(z))

Output: <class 'complex'> 

                   <class 'complex'> 

                   <class 'complex'>

As you can see the same method applies as the previous pieces of code to show what exactly counts as a Complex Number.

-collections/Lists-

within Python Collections/Lists are used to store multiple items within a single variable. Typically Collections/Lists are one of 4 built in data types within Python to be used to store collections of data, while the other 3 Tuple, Set and Dictionary, all of these with different qualities and usage. Collections/Lists are created using square brackets like the following:

Input: thislist = ["Anakin", "Ahsoka", "Obi-Wan"]

               print(thislist)

Output:  ['Anakin', 'Ahsoka', 'Obi-Wan']

Collections/Lists are ordered in the way you input them and are changeable but they also allow for duplicate values within them. Items with in Collections/Lists are indexed, the first item with the Collection/List has index [0], the second item with the Collection/List will have the index of [1] and etc.

In Collections/Lists there is also a way to identify how many items that are contained within a Collection/List, this can be done by using the len() function for example:

Input:  thislist = ["Anakin", "Ahsoka", "Obi-Wan"]

                print(len(thislist))

Output:

As you can see by running this function you can get to see how many items are contained with in the Collection/List.

-loops-

When talking about Loops, Python has two primitive Loop commands that you can use, those commands are "While Loops" and "For Loops".

While Loops can execute statements as long as the conditions with in those statements are true, for example if "2>5" since this statement is incorrect it would not execute. Here is another example of a While Loop:

Input:     i = 1
                  while i < 6:
                        print(i)
                        i += 1

Output: 1

                   2

                   3

                   4

                   5

With While Loops there is one thing to remember which is to increment i because if you do not the loop would not function. In while Loops you can also use the break statement which which can stop a loop even if the condition inside the loop is true. for Example: 

Input:      i = 1
                  while i < 6:
                        print(i)
                        if i == 3:
                           break
                        i += 1

Output: 1 

                   2 

                   3

As you can see by adding break between the last piece and the penultimate piece of code you have created a break within the loop. 

Taking a look at "For Loops" in python, For Loops are used for iterating a sequence that is either a List, a Tuple, a Dictionary, a Set or even a String. With For Loops we are able to execute a set of statements once for each item in a Collection/List, Tuple, Set etc, for example:

Input:  Autobots = ["Cliffjumper", "Hot Rod", "Sideburn"]

               for x in Autobots:

                   print(x) 

Output: Cliffjumper 

                   Hot Rod 

                   Sideburn

When creating a For Loop you do not require to create an indexing variable beforehand. 

Leave a comment

Log in with itch.io to leave a comment.