Python 101 : The Ultimate Python Tutorial For Beginners

Python 101 : The Ultimate Python Tutorial For Beginners

In order to understand the Python language, one must first know of its origin. Python is an interpreted, high-level programming language created by Guido Van Rossum, released in 1991.

It is mainly used in used in web development, game development, data science, artificial intelligence and machine learning.

Features of Python

  1. Open source: Anyone can contribute to Python development
  2. Object Oriented Approach: It uses object oriented language concepts such as objects, classes and encapsulation in providing solutions.
  3. Easy to read and code: Python uses simple syntax which makes it easily understood by developers
  4. Free: Python has no cost of charge.
  5. Interpreted: The source code is executed line by line, not all at once.

In this article I focus on some of the key concepts one needs to grasp. It will be an overview of what to read and learn in Python language.

  • Variables

  • Operators

  • Data Types

  • Built-in Data Structures: Lists, Sets, Tuples and Dictionaries

Variables

These are containers for storing data, created upon assigning a value to a variable. Python has no command for declaring a variable.

full_name = "Immaculate"
print(full_name)

number = "24"
print(number)

Operators

These are categorized into:

  1. Arithmetic
  2. Comparison
  3. Logical

Arithmetic operators are as follows:

  • +: Addition Subtraction
  • *: Multiplication
  • /: Division
  • **: Exponentiation
  • //: Floor Division, rounds down the result of a division
  • %: Modulus, gives you the remainder of a division

Comparison operators are:

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

Logical operators: These are fulfilled when the statements involved fit a certain criteria and return either true or false.

  • and: True only when both statements are true

  • or: False only when both x and y are false

  • not: The not operator simply inverts the input, True becomes False and vice versa.

Data Types

Use the type() function to know which data type a variable is.

String: consists of texts

my_city = "Nairobi"
print(type(my_city))
#<class 'str'>

Numbers: made up of integer, float and complex.

my_int = 18
print(type(my_int))
#<class 'int'>

my_float = 3.14
print(type(my_float))
#<class 'float'>

my_complex_number = 54+7y
print(type(my_complex_number))
#<class 'complex'>

Lists

These are an ordered collection of items, created using [ ], separated by commas. List items are mutable-can change. Also allow duplicated values. List items can be accessed by indexing.

# list with mixed data types
mixed_list = [489, "Moon Knight", 78.9]

# nested list
nest_list = [56.1, [3, 5, 9], ['j']]

Sets

These are unordered collection of items which have no duplicates. The set items are unindexed and unchangeable though one can remove and add new items.

# initialize my_set
my_set = {76, "Python", 5.4}
print(my_set) 
# {5.4, 76, 'Python'}

Tuples

They are similar to lists(ordered, allows duplication of items) but are immutable(cannot add or modify items).

# accessing tuple elements using indexing
my_tuple = ('f','e','r','r','a','r','i')

print(my_tuple[0])   # f
print(my_tuple[5])   # r
print(my_tuple[-3])  # a

Dictionaries

They are unordered data collection of items stored in (key:value) pairs. The values are mutable.

#dictionary with integer keys
my_dict = {1: 'January', 2: 'February'}

# using dict()
my_dict = dict({1:'Pig', 2:'Hen'})