Python – Part #1 (Data Types, Arithmetic Operators & Conditional Statements)

Brief Definition:

Python apart from being the name of a snake type, it’s an interpreted, object-oriented, high-level programming language with dynamic semantics. It’s widely used for data structures, machine learning, artificial intelligence, database management, app development, scripting, web development, etc. It’s an outstanding programming language because its syntax emphasizes readability to make it more understandable for the users.

DATA TYPES: 

In order for the Python interpreter to understand numbers or letters we use the “Data Types” which are a classification of built-in words that can store mathematical numeric values and strings, each one is different and has its logical purpose.

Below are listed the most used ones:

  • Strings: Holds letters (str) | x = “Hello world”
  • Integers: Holds whole numbers (int) | x= 1
  • Float: Holds short decimals (float) | x = 1.5
  • Boolean: Holds either True or False statements (bool) I x = True (or False)

        Casting: Used to temporarily assign different datatypes.

        Note: The pipe sign | represents an alternative.

        x = int(1.5) | x= float( 1) |x = str(1)

        print(x)

        The output will be = 1 | 1.0 | “1”

                String Modification: Are called methods as opposed to functions, they seem the same but

                always belong to a string or any other argument.

                x= “Hi There”

                print(x.upper()) | print(x.lower())

                Output= HI THERE | hi there

There are more alternatives to modify a string, it depends on what you want to do with it.

ARITHMETIC OPERATORS:

Note: ” | ” represents separation.

+ | Addition | x + y

–  | Subtraction | x – y

*  | Multiplication | x * y

/   | Division | x / y

% | Modulus | x % y

** | Exponentiation | x ** y

//   | Floor division | x // y

 

IF STATEMENTS & CONDITIONALS:

Equals | a == b

Not Equals | a != b

Less than | a < b

Greater than | a > b

Less than or equal to | a <= b

Greater than or equal to | a >= b

And | a and b

Or | a or b

Example #1:

We start with “if” which is the principal condition, “elif” continues to other conditions, however, if none of the conditions is true, then we use the “else” statement and it consequently will execute its function.

Therefore this example above will print “x & y are equal”.

 

Example #2:

The following program is intended to compute if the person who is applying for a loan is eligible to have it, we are going to determine it by using the if statement and the operators “and”, “or” & “not”.
 
If the person has a good credit score and high income, the print message will be displayed on the terminal.
If the person does not meet one or neither of the requirements, then the print message won’t be displayed.
 

 

Output: {nothing}
 
If the person has either a good credit score or a high income, then the person will be eligible for the loan.
 
 
Output: You are eligible for a loan
 
If the person has the two requirements and also has no criminal records, then the person will be eligible for the loan.
 

 

Output: You are eligible for a loan.
 
In conclusion, Python’s data types, arithmetic operators, if statements, and conditionals are fundamental building blocks for writing programs in Python.