Introduction

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

Python is different from other programming languages in the following ways:

This guide will include code snippets and outputs in Python as examples of the topic being disscussed.

Normal code will be displayed in this color.
Code with errors will be displayed in this color.
The outputs of the green code will be displayed in this color.
Comments will be displayed in this color.
        
Hello World

Here is what outputting the string "Hello World" looks like in Python:

print("Hello World")
Indentation

Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.

Python uses indentation to indicate a block of code.

Example:

if 5 > 2:
    print("Five is greater than two!")
        

Python will give you a Syntax error if you skip the indentation:

if 5 > 2:
print("Five is greater than two!")
        
Variables

In Python, variables are created when you assign a value to it:

Variables in Python:

x = 5
y = "Hello World"
        
Comments

Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

#This is a comment.
print("Hello World")
        
Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

You can get the data type of any object by using the type() function:

x = 5
print(type(x))
<class 'int'>
        
Lists

A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

thislist = ["apple", "orange", "banana"]
        

You access the list items by referring to the index number:

thislist = ["apple", "orange", "banana"]
print(thislist[0])
apple
        

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.

thislist = ["apple", "orange", "banana"]
print(thislist[-1])
banana
        
If Else Statements

Python supports the usual logical conditions from mathematics:

The if keyword lets you execute specific lines of code only if certain conditions defined by you are met.

The else keyword catches anything which isn't caught by the preceding conditions.

The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
a is greater than b
          
While Loops

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
  print(i)
  i += 1
1
2
3
4
5
For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

thislist = ["apple", "orange", "banana"]
for x in fruits:
  print(x)
apple
orange
banana

Even strings are iterable objects, they contain a sequence of characters:

for x in "banana":
  print(x)
b
a
n
a
n
a
Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function, and it can return data as a result.

In Python a function is defined using the def keyword:

def my_function():
  print("Hello from a function") 
          

To call a function, use the function name followed by parenthesis:

def my_function():
  print("Hello from a function")

my_function()
Hello from a function
          

To customize the outputs of functions, information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

def my_function(name):
  print(name + " is my favorite programming language!") 

my_function("Python")
Python is my favorite programming language!
my_function("Java")
Java is my favorite programming language!
          
Reference

Information on this page can be found on W3 Schools