Introduction

What is Python?

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

It is used for:

  • web development (server-side),
  • software development,
  • mathematics,
  • system scripting.

What can Python do?

Python can be used on a server to create web applications.

Python can be used on a server to create web applications.

Python can be used on a server to create web applications.

Python can be used on a server to create web applications.

Python can be used on a server to create web applications.

  • Python can be used on a server to create web applications.
  • Python can be used alongside software to create workflows.
  • Python can connect to database systems.
  • It can also read and modify files.
  • Python can be used to handle big data and perform complex mathematics.
  • Python can be used for rapid prototyping, or for production-ready software development.
  • Why Python?

    • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
    • Python has a simple syntax similar to the English language.
    • Python has syntax 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-orientated way or a functional way.
    Python getting started

    Python Install

    Many PCs and Macs will have python already installed.

    To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):

    C:\Users\Your Name>python --version

    To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:

    python --version

    If you find that you do not have python installed on your computer, then you can download it for free from the following website: https://www.python.org/

    Python Quickstart

    Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.

    The way to run a python file is like this on the command line:

    C:\Users\Your Name>python helloworld.py

    Where "helloworld.py" is the name of your python file.

    Let's write our first Python file, called helloworld.py, which can be done in any text editor.

    print("Hello, World!")

    Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:

    C:\Users\Your Name>python helloworld.py

    The output should read:

    Hello, World!

    Congratulations, you have written and executed your first Python program.

    Python syntax

    Execute Python Syntax

    Python syntax can be executed by writing directly in the Command Line:

    >>> print("Hello, World!")
    Hello, World!

    Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:

    C:\Users\Your Name>python myfile.py

    Python Indentations

    Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.
    Python uses indentation to indicate a block of code.

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

    Python will give you an error if you skip the indentation:

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

    Creating Variables

    Unlike other programming languages, Python has no command for declaring a variable.
    A variable is created the moment you first assign a value to it.

    x = 5
    y = "John"
    print(x)
    print(y)

    Variables do not need to be declared with any particular type and can even change type after they have been set.

    x = 4 # x is of type int
    x = "Sally" # x is now of type str
    print(x)

    Variable Names

    A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:

    • A variable name must start with a letter or the underscore character
    • A variable name cannot start with a number
    • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
    • Variable names are case-sensitive (age, Age and AGE are three different variables)
    Python numbers

    There are three numeric types in Python:

    • int
    • float
    • complex

    Variables of numeric types are created when you assign a value to them:

    x = 1 # int
    y = 2.8 # float
    z = 1j # complex

    Int

    Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

    x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z))

    Float

    Float, or "floating point number" is a number, positive or negative, containing one or more decimals.

    x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z))

    Complex

    Complex numbers are written with a "j" as the imaginary part:

    x = 3+5j y = 5j z = -5j print(type(x)) print(type(y)) print(type(z))
    Python strings

    String Literals

    String literals in python are surrounded by either single quotation marks, or double quotation marks.
    'hello' is the same as "hello".
    Strings can be output to screen using the print function. For example: print("hello").
    Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

    Example

    Substring. Get the characters from position 2 to position 5 (not included):

    a = "Hello, World!" print(a[1])

    Example

    The strip() method removes any whitespace from the beginning or the end:

    a = " Hello, World! " print(a.strip()) # returns "Hello, World!"

    Example

    The len() method returns the length of a string:

    a = "Hello, World!" print(len(a))