DEV Community

Cover image for Python 101: Ultimate Python Guide
Claire Maina
Claire Maina

Posted on • Updated on

Python 101: Ultimate Python Guide

Python 101: Ultimate Python Guide

Introduction to Python

Python is an interpreted, interactive, object-oriented programming language. It is a popular programming language created by Guido van Rossum, and released in 1991.

It is used for:

  • Web development
  • Desktop Application
  • Automation
  • Data science, Artificial Intelligence, Machine Learning

Why Python?

Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). It has a simple syntax similar to the English language that allows developers to write programs with fewer lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way. It uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
It relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
Python programs have the extension .py and can be run from the command line by typing python file_name.py.

Concepts we will be going through today:

  1. How to install Python
  2. Your first python program
  3. Comments
  4. Variables
  5. Arithmetic Operators
  6. Comparison Operators
  7. Logical Operators
  8. Control Flow and Conditional operators

1) How to install Python
The most recent major version of Python is Python 3, which we shall be using. In this tutorial, Python will be written in a text editor. It is possible to write Python in an Integrated Development Environment, such as VSCode, Pycharm, Atom, Netbeans or Eclipse which are particularly useful when managing larger collections of Python files.
Here is a guide on how to install python on your machine.

Here is a guide on how to install VSCode.

2) Your first python program
Create a new folder in your machine where you will be saving your python files, for example PythonProjects. Launch the VS code and open the new folder you created, PythonProjects. Create a new python file, helloworld.py file and enter the code below.

image

print() is a built-in function in Python.

3) Comments
You can add comments to your Python scripts. Comments are important to make sure that you and others can understand what your code is about. To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they will not influence your result.
image

4) Variables
Variables are containers for storing data values. Python has no command for declaring a variable. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String. Note that variables are case-sensitive and the first character can either be character or underscore and not a digit. Once you create such a variable, you can later call up its value by typing the variable name.
image

Output
image

5) Arithmetic Operators
They are used to perform mathematical operations like addition, subtraction, multiplication, and division.
image

Output
image

6) Comparison Operators
Compares the values. It either returns True or False according to the condition.
image

Output
image

7) Logical Operators
They perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements.
image

Output
image

8) Control Flow and Conditional operators
So far the code has consisted of sequential execution, in which statements are always performed one after the next, in exactly the order specified. Frequently, a program needs to skip over some statements, execute a series of statements repetitively, or choose between alternate sets of statements to execute. That is where control structures come in. A control structure directs the order of execution of the statements in a program (referred to as the program’s control flow).

  • if Statement

if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise it will not. As we know, python uses indentation to identify a block.
Let us use a real-life scenario “If the weather is sunny, I will not wear a jacket”
image

Output
image

  • if-else Statement

Example: “If the weather is not sunny, wear a jacket”
image

Output
image

  • elif statement

Example: “If the weather is sunny, no jacket, else if(elif) the weather is cold, wear a jacket, else wear a light jacket”.
image
Output
image

Top comments (0)