Python: The simplest introduction

Hello, so I got inspired to start blogging following a video from Adrian Twarog. In my first article I will do a quick introduction to the Python language.

Python is a multi-purpose programming language used for Machine Learning, Artificial Intelligence, Data Science, Web development, Automation, etc.

Variables

Variables are used to temporarily store data in a computer's memory. To declare a variable in Python we type a name for that variable, then we add an equal sign and then we type a value. Some examples:

name = "Sam" # String
age = "22" # Integer
price = 15.34 # Float
is_awake = False # Boolean

Note: Comments in Python are written starting with a # symbol.

Inputs

In Python we use input() to ask for a value in the terminal window:

name = input("What's your name? ")
print("Hello " + name)

image.png

Type conversion

The three main types of data in python are numbers, strings and booleans. If you want to convert the value of a variable from one type to another:

number_string = "12"
print(type(number_string)) # Type: string
number_int = int(number_string)
print(type(number_int)) # Type: int

image.png

The built-in functions to convert types of data are the following:

int() # Convert to Integer (Whole numbers)
float() # Convert to Float (Decimal numbers)
bool() # Convert to Boolean (True/False)
str() # Convert to String of characters

Strings

Strings are arrays of characters. Some of the strings functions are:

str.upper()
str.find("y")
str.replace("Hello", "Bye")

Arithmetic operators

The arithmetic operators are used to perform operations on variables and values:

print(10+3) # Addition
print(10-3) # Substraction
print(10*3) # Multiplication
print(10/3) # Division (Float result)
print(10//3) # Division (Int result)
print(10%3) # Remainder of the division
print(10**3) # Exponents

If you want to modify the value of a variable with arithmetic operators you can use this expresion:

x = 10
x = x + 3
x += 3 # Same as the second line

Note: Operator precedence in Python: () Parentheses > * Exponent > , /, //, % Multiplication, Division, Floor division, Modulus > +, - Addition, Subtraction

Comparison operators

Or relational operators, they are used to compare values:

> # Greater than
< #Less than
>=  # Greater than or equal to
<= # Less than or equal to
== # Equal
!= # Not equal

Logical operators

The logical operators are used to combine conditional statements:

and # True if both statements are true
or # True if at least one statement is true
not # Reverses the result

If statements

Using the logical operators we can write If statements:

a = 10
b = 20
if b>a:
 print("b is greater than a")

Note: Python uses indentation (The space before the print line) where other programming languages use curly-brackets.

While loops

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

Note: If you don't increment (i += 1), the loop will continue indefinitely.

Lists

Lists are used to store mutliple items in a single variable and are created using square brackets:

makes = ["lamborghini", "porsche", "chevrolet"]
  • The items inside a list are indexed, with the first item having the index [0].
  • The last item has the index [-1].
  • Lists can contain different data types.

Some methods with lists:

append(item) #Adds item to the end of the list
insert(index, item) #Adds item in the index
remove(item) # Removes the item
clear() # Empties the list
in # States if the item exists in the list
len() # Returns the number of items in the list

For loops

For is used to iterate over a sequence, like a list.

makes = ["lamborghini", "porsche", "chevrolet"]
for x in makes:
  print(x)

For also works for strings:

for x in "lamborghini":
  print(x)

Range() function

Range() returns a sequence of numbers, starting from 0 by default, and incrementing by 1 (Step) by default aswell.

range(start, stop, step)

x = range(3, 20, 2)
for n in x:
  print(n)

Note: this function stops at 19 rather than 20.

Tuples

Tuples are similar to lists but unchangeable and written with round brackets.

makes_tuple = ("lamborghini", "porsche", "chevrolet")
makes_tuple.count() # Returns the number of ocurrences of an element
makes_tuple.index() # Returns the index of the first ocurrence of an element