Introduction

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.

Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.

Guido van Rossum began working on Python in the late 1980s as a successor to the ABC programming language and first released it in 1991 as Python 0.9.0. Python 2.0 was released in 2000 and introduced new features such as list comprehensions, cycle-detecting garbage collection, reference counting, and Unicode support. Python 3.0, released in 2008, was a major revision that is not completely backward-compatible with earlier versions. Python 2 was discontinued with version 2.7.18 in 2020.

Python consistently ranks as one of the most popular programming languages.

Hello, World!

Python is a very simple language, and has a very straightforward syntax. It encourages programmers to program without boilerplate (prepared) code. The simplest directive in Python is the "print" directive - it simply prints out a line (and also includes a newline, unlike in C).

There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. This tutorial uses Python 3, because it more semantically correct and supports newer features. For example, one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses.

To print a string in Python 3, just write:

print("This line will be printed.")
Variables and Types

Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

This tutorial will go over a few basic types of variables.

  • Numbers
  • Python supports two types of numbers - integers(whole numbers) and floating point numbers(decimals). (It also supports complex numbers, which will not be explained in this tutorial).

    To define an integer, use the following syntax:

    myint = 7
    print(myint)

    To define a floating point number, you may use one of the following notations:

    myfloat = 7.0
    print(myfloat)
    myfloat = float(7)
    print(myfloat)
  • Strings
  • Strings are defined either with a single quote or a double quotes.

    mystring = 'hello'
    print(mystring)
    mystring = "hello"
    print(mystring)
    Conditions

    Python uses boolean logic to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.

  • Boolean operators
  • The "and" and "or" boolean operators allow building complex boolean expressions, for example:

    name = "John" age = 23
    if name == "John" and age == 23:
    print("Your name is John, and you are also 23 years old.")
    if name == "John" or name == "Rick":
    ("Your name is either John or Rick.")
  • The "in" operator
  • The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list:

    name = "John"
    if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")

    Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.

    Here is an example for using Python's "if" statement using code blocks:

    statement = False
    another_statement = True
    if statement is True:
    # do something
    pass
    elif another_statement is True: # else if
    # do something else
    pass
    else:
    # do another thing
    pass
  • The 'is' operator
  • Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves. For example:

    x = [1,2,3]
    y = [1,2,3]
    print(x == y) # Prints out True
    print(x is y) # Prints out False
  • The "not" operator
  • Using "not" before a boolean expression inverts it:

    print(not False) # Prints out True
    print((not False) == (False)) # Prints out False
    Loops

    There are two types of loops in Python, for and while.

  • The "for" loop
  • For loops iterate over a given sequence. Here is an example:

    primes = [2, 3, 5, 7]
    for prime in primes:
    print(prime)
  • "while" loops
  • While loops repeat as long as a certain boolean condition is met. For example:

    # Prints out 0,1,2,3,4

    count = 0
    while count < 5:
    print(count)
    count += 1 # This is the same as count = count + 1
  • "break" and "continue" statements
  • "break" is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples:

    # Prints out 0,1,2,3,4

    count = 0
    while True:
    (count)
    count += 1
    if count >= 5:
    break

    # Prints out only odd numbers - 1,3,5,7,9
    for x in range(10):
    # Check if x is even
    if x % 2 == 0:
    continue
    print(x)
    Reference
  • All the documentation in this page is taken from learnpython.org.