Quick Notes

Notes from www.learnpython.org

Syntax

Strings

Strings are defined either with a single quote or a double quotes.

Strings are bits of text. They can be defined as anything between quotes.

You can also use single quotes to assign a string. However, you will face problems if the value to be assigned itself contains single quotes.

The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)

There are additional variations on defining strings that make it easier to include things such as carriage returns, backslashes and Unicode characters. These are beyond the scope of this tutorial, but are covered in the Python documentation.

Simple operators can be executed on numbers and strings

Assignments can be done on more than one variable "simultaneously" on the same line

Mixing operators between numbers and strings is not supported

Lists

Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner.

Accessing an index which does not exist generates an exception (an error).

Arithmetic Operators

The addition, subtraction, multiplication, and division operators can be used with numbers.

Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder.

Using two multiplication symbols makes a power relationship. 7**2 is 7 squared

Using Operators with Strings

Python supports concatenating strings using the addition operator

Python also supports multiplying strings to form a string with a repeating sequence

Lists can be joined with the addition operators

Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator

String Formatting

Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".

To use two or more argument specifiers, use a tuple (parentheses)

Any object which is not a string can be formatted using the %s operator as well. The string which returns from the "repr" method of that object is formatted as the string.

Here are some basic argument specifiers:

%s - String (or any object with a string representation, like numbers)

%d - Integers

%f - Floating point numbers

%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.

%x/%X - Integers in hex representation (lowercase/uppercase)

Example:

data = ("Martian", "from CyberSpace", 53.44)
format_string = "Hello %s %s. Your current balance is $%s."

print(format_string % data)

Conditions

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

Variable assignment is done using a single equals operator "=", whereas comparison between two variables is done using the double equals operator "==". The "not equals" operator is marked as "!=".

Boolean operators

The "and" and "or" boolean operators allow building complex boolean expressions

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.

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. Code blocks do not need any termination.

A statement is evaluated as true if one of the following is correct:

1. The "True" Boolean variable is given, or calculated using an expression, such as an arithmetic comparison.

2. An object which is not considered "empty" is passed.

The 'is' operator

Unlike the double equals operator "==", the "is" operator does not match the values of the variables, but the instances themselves.

The "not" operator

Using "not" before a boolean expression inverts it.

Loops

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

The "for" loop

For loops iterate over a given sequence.

For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). The range function is zero based.

"while" loops

While loops repeat as long as a certain boolean condition is met.

"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.

Can we use "else" clause for loops?

Unlike languages like C,CPP.. we can use else for loops. When the loop condition of "for" or "while" statement fails then code part in "else" is executed. If a break statement is executed inside the for loop then the "else" part is skipped. Note that the "else" part is executed even if there is a continue statement.

Last updated