python tutorial

Intro to Python Programming

Learn more about Python, one of the world’s most versatile and popular programming languages.

Before you can start programming with Python, there are a number of general programming fundamentals that are important to understand. Let’s take a closer look at the basics.


Python Programming Language Basics

Programming languages tend to share some foundational concepts and general building blocks. By learning these concepts, we can gain a better understanding of any new programming language that we might come across in the future.

With that being said, it is very important to have a solid understanding of the basics of programming languages before starting to write code. In most cases, only the process of writing code and executing it will change between individual languages. Let’s dive into these building blocks and first deepen our understanding of syntax and semantics:

Python Syntax

Syntax defines a set of rules and a combination of various symbols. Syntax is followed to write properly structured code that can be accepted by the language, before it is issued as instructions for the computer to run. Programming languages can vary in the syntax used, and code is written in a document with a particular file extension (e.g. for Python language code is written in a file with a ‘.py’ extension). If the code written is not valid syntactically, the document will show “syntax error”. We’ll explore Python syntax in the upcoming lessons.

Python Semantics

The semantics of a language are responsible for defining the meaning behind the syntax. In other words, it’s not enough to write a sentence with the proper punctuation and grammar, and order of words if it doesn’t mean anything. For example, the sentence “The desk ate the color blue.”, is syntactically correct, but doesn’t have any meaning. The same goes for programming. While code might be written with the right syntax in mind, the end result has to mean something.

For our purposes, semantics describes a set of processes a computer will go through while executing the code in a particular language. This could be determining what output a program should produce for a given set of inputs or it could be just describing the process of how a code will be run on different platforms. Semantic analysis takes any code written and determines whether or not the code consists of valid strings. If not, the code would not be run and meaningful errors will be thrown to tell the programmer that the code contains errors.

In this lesson, we will go through a quick introduction to Python as a programming language.

Note: We will be using Python 3 for programming in Python.


How to Write Python Code

While Python is one of the most beginner-friendly programming languages in use, it’s a good idea to understand some core principles before getting started. Python programming courses, for example, have grown in popularity, as they can quickly get you up and running with the language.

It’s also a good idea to take advantage of the wealth of material available online. There are a number of PEP documents, for example, which can be invaluable for beginners. PEP stands for Python Enhancement Proposal, and these documents describe new features and aspects of Python, including documents, design, and style.

PEP 8 is a readily-available document that aims to improve the readability and consistency of Python code – it also provides guidelines and best practices on how to write Python code.

Python Expressions or Statements

A Python expression or a statement is a line of code or building block that is syntactically correct and is supposed to carry out a certain action when executed.

Indentation in Python

Indentation, also referred to as leading whitespace, is an important element of Python. The way that lines of code are indented in Python determines how statements are grouped together.

​​PEP 8 includes two key indentation rules:

  • Make sure to use four consecutive spaces to indicate indentation.
  • Use spaces instead of tabs.

Comments in Python

Comments are essential building blocks of any programming language. Comments, unlike other programming features, are not executed and are used to explain your logic and assumptions in code where it might not be obvious to people reading the code. Comments are also used to add documentation to your code so that developers re-using your code know why and how to use that piece of code.

Comments should always answer the “why?” question. Why did you implement your code in that particular way or why was some assumption made? This helps developers or teammates reading your code easily understand the reasoning behind your implementation. Comments are also used to comment out lines of actual code that you don’t want to execute.

Let’s take a look at how Python as a programming language allows developers to use comments in their code.


Single-line comment using a hashtag sign #

Developers can add comments in Python by starting with a number or hashtag sign #. Anything that follows # sign is considered as a comment and Python Interpreter will ignore and not execute it.

Python
# comment in Python starts with a hashtag
# comment is not executed in Python
# The leading >>> output prompt does not need to be typed out
print("Python Interpreter prints this text as it is a line of code!")
# below is a line of code that is commented out using comments
# and it will not get executed
# print("this line of code is commented out by starting with #")
Output
Python Interpreter prints this text as it is a line of code!

Multi-line comment using a triple quote

Python doesn’t really allow multi-line comments. However, even though it is not intuitive, a developer can use multi-line string (triple-quotes “””) to create a comment that spans multiple lines.

"""
This is a multi-line string
in Python that acts as a 
multi-line comment if not assigned to a
variable. It comes really handy when you
have to use a comment for a lengthy description
"""

Mathematical Operators in Python

There are many mathematical operators that Python provides that are useful as part of writing code. Addition +, Subtraction -, Multiplication *, Division /, Modulus or Remainder % and Power or “Raised To” ** are some of the mathematical operators that Python provides to developers.

Python
print(2+2)       # prints 4
print(10 - 5)    # prints 5
print(4 * 8)      # prints 32
print(2 / 5)      # prints 0.4
print(8 % 4)    # remainder when 8 is divided by 4, prints 0
print(8 % 3)    # remainder when 8 is divided by 3, prints 2
print(2 ** 4)    # 2 * 2 * 2 * 2, prints 16
print(3 ** 2)    # 3 * 3, prints 9
Output
4
5
32
0.4
0
2
16
9

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required