Python Language

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

Printing done with print() function in Python 3 (it uses to be a statement in Python 2).

print('something')

Comments

# This is a comment

Reserved Words

Reserved words can only be used to mean the thing Python expects them to mean. They cannot be used as variable names, function names, class names, or identifiers.

False class return is finally
None if for lambda continue
True def from while nonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise

Constants

Constants are fixed values, they do not change throughout the program. Constants can be numeric (integers or floating point numbers), or strings, which can be single quoted or double quoted. Constants can be assigned to variables, can be arguments of functions. Constants have a type.

Variables

Variables are memory locations used to store values, and have labels associated to them , the variable name. Variables are declared and assigned a value though an assignment statement. Variables have a type.

a = 1
b = 'something'
print(a)
print(b)

Variable Naming Rules

Variable names are case sensitive. Variable names can start with letters or underscore ('_') - but underscores should be generally avoided because Python tends to use underscores for its internal purposes. The rest of the variable name can be letters, numbers and underscores. No other characters are allowed. Variable names should be sensible (mnemonic).

Literals

Literals have a type.

Type

The type of a variable or a constant can be obtained with the built-in function type()

Types

Integers

Whole numbers, expressed as numeric constants that do not contain a decimal point.

x = -20
type(x)
<class 'int'>

Floating Point Numbers

Numbers with a decimal point.

x = 98.6
type(x)
<class 'float'>

String

Single or double quotes sequence of characters.

x = 'abc'
type(x)
<class 'str'>

Type Conversions

There are built-in function that can be used for type conversion:

  • float()
  • int(). int() can be called on a float or on a string.
  • str()

Statements

In Python 2, print used to be a statement, while in Python 3, print() is a function.

Assignment Statement

The assignment statement assigns a value to a variable.

x = 1

The assignment statement accepts expressions:

x = x + 1

Expressions

Numeric expressions. Order of evaluation takes into account operator precedence.

Operators

+ Addition For numbers, adds them together, for strings, it concatenates.
- Subtraction
* Multiplication
/ Division In Python 3 integer division converts to floating point (not the case in Python 2, which truncates).
** Power (exponentiation)
% Remainder (modulo)
= Assignment

Operator Precedence

The following rules apply, and they are specified in the order of their descending precedence:

  • Parentheses are always respected.
  • Exponentiation.
  • Multiplication, division and remainder.
  • Addition and subtraction.
  • For operators with the same precedence, proceed left to right.

Functions

Built-in Functions

  • type() returns the type of the argument.
  • print(). If more comma-separated arguments are used, every comma adds a space.
  • input() instructs Python to pause and read data from stdin. input() returns a string.
s = input('this is the prompt')
print(s)

Identifiers

Python Script

A Python program file is called a Python script - a stored set of instructions that can be handed over to the Python interpreter. Python scripts have the .py extensions.

Flow Control

Sequential Steps

Sequential steps have the same indentation level.

Conditional Steps

if x < 10:
  print('something')
if a == 1:
  print('something')
else:
  print('something else')

Loops

n = 5
while n > 0:
  print(n)
  n = n - 1
while <condition>:
  code-block

while x < 5:
  x = x + 1
  print

Loops have iteration variables, which are initialized, checked and changed within the loop.

Functions

Classes

Traceback

This means Python quit somewhere.

Organizatorium

Nesting