2. Language Overview

2.1 Overview

  • doesn’t use braces or semicolon

  • line endings and indentation are meaningful

  • everything is an object

  • How do all these pieces fit together?

2.2 Hello world

  • hello.py is how you check your installation is working.

2.3 Anatomy

  • See hello_version.py.

    • comments start with #.

    • first comment is a special case: Shebang/ hash bang

  • Shebang line

    • common pattern for unix based systems

    • example: #!/usr/bin/env python3

    • #! must be the first two characters.

    • path to the interpreter is specified following this.

    • this is useful when you have several installations of python

  • import tells the interpreter to import a module from the library

  • print is a function in python 3.

    • prefered format is print('This is python version {}'.format(platform.python_version()))

  • Why do python scripts have if __name__ == '__main__':?

    • By having this statement, it forces the executor to read the entire script before executing what’s in the conditional.

    • Python requires that a function is defined before it’s called.

2.4 Statements and expressions

  • Statement: unit of execution

  • Expression: unit of evaluation

  • Expressions:

    • x = y

    • x*y

    • (x, y)

    • x

    • True

    • f()

  • Statement: is a line of code(?)

    • version = platform.python_version() is a statement and an expression(?)

2.5 White space

  • White space is significant in python (unlike most other languages)

  • example:

def main():
    message()
def message():
    print('line 1')
    print('line 2')
if __name__ == '__main__': main()



is completely different from

def main():
    message()
def message():
    print('line 1')
print('line 2')
if __name__ == '__main__': main()

  • Python comments end at the end of a line

2.6 Print

  • print('Hello, World.'). Hello, World is a literal string.

  • to print a variable,

  • print('Hello, World. {}'.format(x)) Format is covered in detail later.

    • format is a method of the string object, nothing to do with the print function.

  • print('Hello, World. %d' %x) is legacy usage. It will be deprecated in future versions of python.

  • Beginning python 3.6, a simpler format is:

    • print(f'Hello, Word. {x}')

2.7 Blocks

  • in Python, blocks are delimited by indentation.

  • example:

if x < y:
    print('x < y: x is {} and y is {}'.format(x, y))
  • the indentation specifies a block. All statements with indentation come under the conditional statement. If unindented, the statement is outside the conditional block.

  • they all have to be indented the same level (same number of characters)

  • there are no end statements in python.

  • Blocks do not define the scope of the variable in python.

    • even if a variable is defined inside a block, its scope is still the same outside of it.

2.8 Conditionals

if x < y:
    print('x < y: x is {} and y is {}'.format(x, y))
elif y > x:
    print('x < y: x is {} and y is {}'.format(x, y))
else:
    print('do something else)

is the Python way.

  • if x < y: print('x < y: x is {} and y is {}'.format(x, y)) also works but frowned upon.

  • Python doesn’t have switch case since you can do that with a string of elifs

2.9 Loops

  • Python provides:

    • a while loop and (until a condition is satisfied)

    • a for loop (over a sequence)

  • while:

a, b = 0, 1
while b < 1000:
    print(b)
    a, b = b, a + b
  • for:

for i in words:
    print(i)

2.10 Functions

  • a function is defined using the keyword def.

  • arguments to the function separated by commas

    • argument becomes a variable within the scope of that function

    • paranthesis are not optional even if there are no arguments

def isprime(n):
    if n <= 1:
        return False
    for x in range(2, n):
        if n % x == 0:
            return False
    else:
        return True
<br>
n = 5
if isprime(n):
    print(f'{n} is prime')
else:
    print(f'{n} not prime')

2.11 Objects

  • In Python, a class is a definition

  • An object is an instance of a class.

class Duck:
    def quack(self):
        print('Quaaack!')
    def walk(self):
        print('Walks like a duck.')
  • The above is the definition of a class, and the two functions are part of that class. Sometimes these are referred to as methods.

  • the first argument for a method in a class is always self. It’s not a keyword, but it’s the norm.

def main():
    donald = Duck()
    donald.quack()
    donald.walk()
  • donald is an object of the class Duck.

  • . is used to reference members of the object donald.

if __name__ == '__main__': 
    main()