DEV Community

Cover image for PYTHON 101: ULTIMATE GUIDE TO PYTHON
Andisi (Roseland) Ambuku
Andisi (Roseland) Ambuku

Posted on • Updated on

PYTHON 101: ULTIMATE GUIDE TO PYTHON

What is Python?

Python is a general-purpose, interpreted, high-level programming language. It is a mouthful of a definition so let’s break it down;

  • General-purpose means you can use Python in fields like automation, web applications, and data science.
  • Interpreted means that you write Python code in a file called a source code and then the Python interpreter changes the source code to machine-readable code, a line at a time.
  • High-level means that this language is simple to learn as you do not need to know the particulars of the computer to develop programs using Python.

Features of Python

  • Open Source: Python is free and easily accessible via their official website and being open source means that the source code that makes the language is available to the general public. You can access it on the Python Website.
  • Object-Oriented Language: Python supports the use of objects and classes to write programs and build applications.
  • Simplicity: Python is high-level programming and this makes it easy to understand, learn and code in.
  • Portability: Python is the portable meaning if there is Python code on a Windows machine it can run on another machine with a different operating system like Linux.
  • Dynamically Typed: this means that there is no need to specify the type for example int for an integer value as it is decided at the run time of the program.

Installation

  1. Download the latest version here.
  2. Once downloaded, add Python to the path variable in your machine and
  3. Follow the prompts to install it.
  4. Choose a code editor such as VS Code, PyCharm, Kite, or Sublime Text.

Now you are ready to begin coding in Python!

Note: It is better to select a Python version that has security as opposed to those with bug fixes. It ensures the security of the programs you will write. At the time of writing this article, it is version 3.8.

Syntax in Python

Comments
These describe what a piece of code does and why it was written. The Python interpreter ignores comments when it executes the code.

# This is a comment on a single line.
Enter fullscreen mode Exit fullscreen mode

Identifiers
These are names that identify objects such as functions and variables. The name of an identifier needs to begin with a letter or an underscore ( _ ).

Keywords
These are words with special meaning and cannot be used for any other purpose other than what they stand for.
Examples

  • False
  • class
  • finally
  • return
  • global

Literals
These are the values that are assigned to variables or constants. There are four types which are:

  • String literals
  • Numeric literals
  • Boolean literals
  • Special literals

Indentation and White spaces
In Python indentation and white spaces are used to create the code structure.
Example

def value():
    i = 13
    max = 100
    while (i < max):
        print(i)
        i = i + 1

# call function value
value()
Enter fullscreen mode Exit fullscreen mode

Variables
A variable is a name that is used to specify a location in memory. When you first assign a value to a variable it is created and stored in memory. As you continue creating your program you can change the value of the variable.
Example

middle_name = 'Andisi'
Enter fullscreen mode Exit fullscreen mode

Standards for creating variables in Python

  • A variable must begin with an underscore or a letter.
  • The keywords in Python cannot be used to name a variable.
  • Variable names are case-sensitive for example, food, Food, and FOOD are three different variables.
  • A variable name cannot begin with a number.
  • A variable name can only contain underscores and alphanumeric characters (0-9, a-z, and _ ).

Operators
Operators are used in performing operations on values and variables.

  • Arithmetic operators – These are used to perform mathematical operations on numerical values Addition + Subtraction - Multiplication * Division / Modulus %
  • Assignment operators – these are used in assigning values to variables. See example at W3 Schools
  • Comparison operators – these are used in comparing two values Equal = = Not equal ! = Greater than > Less than < Greater than or equal to < = Less than or equal to >=
  • Logical operators – these are used in combining conditional statements. They include, and, or, not.

Data types
A data type is the classification of data in Python. The following are the standard datatypes in python:

  • Text – this is a data type that consists of text written as a string. It is written using single, double, and triple quotes. Example
learning = 'My first string'
Enter fullscreen mode Exit fullscreen mode
  • Numeric – this is a data type that represents numbers. They include:

Integers – These are positive or negative whole numbers. They are indicated using int.
Float – These are numerical values with decimal points. They are indicated using float
Complex numbers – These are complex class values. They look like this, (real part) + (imaginary part)k for example 3+4k. They are indicated using complex

  • Sequence – This is a data type that is a collection of ordered similar or different data types. The sequence stores multiple values in an ordered way. List – This is an ordered and modifiable collection of data that allows duplicate values.
fruits = ['mango', 'orange', 'grapes', 'pineapple']
Enter fullscreen mode Exit fullscreen mode

Tuple – This is an ordered and unmodifiable collection of data that allows duplicate values.

drinks = ('water', 'soda', 'mocktails', 'yogurt')
Enter fullscreen mode Exit fullscreen mode
  • Mapping – This is a data type that is used to store values much like a map. Dictionary -This contains key and value pairs. It is an unordered and unmodifiable collection of values.
person = {'name':'Andisi', 'gender' :'female', 'country': 'Kenya',}
Enter fullscreen mode Exit fullscreen mode

Set – This is an unordered collection of values that is mutable. It is indicated as 'set'
Example

pets = {'cat', 'dog', 'parrot', 'fish'}
Enter fullscreen mode Exit fullscreen mode
  • Boolean – This data type has two values, true or false. Example

Control Flow
This is the order of code execution in a program to arrive at various specified decisions. The control flow is made possible using if statements, loops, and function calls.

  • If... Else statements
r = 4
if r < 0:
    print('r is a negative number')
else:
    print('r is a positive number')

Enter fullscreen mode Exit fullscreen mode
  • For loops
fruits =['mango', 'orange', 'grapes', 'pineapple']
for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode
  • While loops
t = 0
while t < 6:
    print(t)
    t = t + 1
Enter fullscreen mode Exit fullscreen mode

Classes
A class is a “blueprint” for creating objects. Python is an object-oriented programming language and as such everything is an object and they have methods and properties. Classes are created to create objects.
Example

class Human:
    pass
print(Human)
Enter fullscreen mode Exit fullscreen mode

Until next time, may the code be with you!

Top comments (0)