Our Only Rule

The rules applied to sample codes are as follows (its only one): The syntax used is that of Python 3 but whenever there is a difference in the syntax of Python 3 and 2, it will be stated for beginners to quickly learn the differences between the two versions without having to read any other material on that. With that in mind, let's Python.

You can use our online editor here to practice basic code, Click and it will open in another tab, practice along, it helps

Identifiers/Variables

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. Assigning Values to Variables: Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the equal to (= )operator is the name of the variable and the operand to the right of the  equal to (=) operator is the value stored in the variable. For example:

name = "Theophilus"
age = 20
weight = 55.5
items_to_print = ['card','flyer','slides']

Variable Naming Rules

We just saw how easy it is to create variables but if you watched closely they seem to follow certain rules. Take a look at how we created the last variable: items_to_print Do you think I used the underscores for fun or you are guessing it's one of the rules? You guessed right, you can't use spaces in variable naming So here are the rules, no one will beat you for not obeying them, but Python will not understand you and will throw you errors if you don't follow them.
  1. Special characters are not allowed in naming variables. Yeah, but the underscore is allowed, a cheat?
  2. Punctuation marks are not allowed in it.
  3. Operations are not allowed since Python might try computing things for seeing operations.
  4. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass,var_1 and  print_this_to_screen, all are valid examples.
  5. An identifier cannot start with a digit - 1variable is invalid, but variable1 perfectly fine.
  6. You cannot give a variable name that Python has reserved for itself for your own.
Yeah, of course, you need to know these reserved Keywords.


Python's reserved keywords

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

 

These are words that have been predefined for the language, which means Python has a certain meaning attached to these names already, so using these names will be confusing Python and therefore are not allowed. You do not need to remember all of them, you will get to know as you practice.

 

Now, let us create some variables:

counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string

print (counter)
print (miles)
print (name)
    

Here, 100, 1000.0 and "John" are the values assigned to counter, miles and name variables, respectively. While running this program, this will produce the following result:

    100
    1000.0
    John

Multiple Assignment: Python allows you to assign a single value to several variables simultaneously. For example:

 >>> a = b = c = 1

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example:

	a, b, c = 1, 2, "john"

Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c.

Standard Data Types: The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard types that are used to define the operations possible on them and the storage method for each of them. Python has five standard data types:

Python Numbers

Number data types store numeric values. They are immutable data types which means that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example:

    var1 = 1
    var2 = 10

You can also delete the reference to a number object by using the del statement. The syntax of the del statement is:

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example:

    del var
    del var_a, var_b

Python supports four different numerical types:

Examples

Here are some examples of numbers:

int

int
long
float
Complex
10
51924361L
0.0
3.14j
100
-0x19323L
15.20
45.j
-786
0122L
9.322e
-36j
080
0xDEFABCECBDAECBFBAEl
32.3+e18
.876j
-0490
535633629843L
-90.
3e+26J
-0x260
-052318172735L
-32.54e100
4.53e-7j
0x69
-4721885298529L
70.2-E12
-.6545+0J



Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.

A complex number consists of an ordered pair of real floating-point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number.

Read More...

Data Type Conversion

Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function. There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.


Function
Description
int(x [,base])
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
float(x)
Converts x to a floating-point number.
complex(real [,imag])
Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
 Converts s to a tuple.
list(s
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
oct(x)
Converts an integer to an octal string.

 

 

Take Test

Let's get an introduction to strings. Strings are always chain of characters bounded by triple, double quotes or single quotes in Python, a string can also be just a character.

"Hello World"
"12345"
"False"
'my mom is going to school'
Are all strings.

In order to tell the computer to output a string to the screen, just write out *print* and between your parenthesis/brackets, type your string. This applies to all the data types we will look at.

Accessing Strings

To obtain sub-strings, indexing or slicing is used. Square brackets are used and the position (index) of the sub-string or sub-strings is entered within the square brackets; Python then returns the sub-string or sub-strings found at this index. Put this behind your mind, Python treats strings as lists when it comes to slicing and indexing. Let's look at a practical example: From our previous lesson Variable Naming, let's give our fourth string example a name and then work on it using indexing and slicing.

sentence = 'my mom is going to school'

NB: I gave it the name sentence which is reasonable enough for what the string says. This string which we have just given the name sentence has a length not equal to the number of letters in it but every character counts. Space is also a character, so guess what the length or number of characters in it will be. We can easily do this by using a method called len on the string sentence.

print(len(sentence))
This should print out:
>> 25

Now, say we want to get only the first letter of the sentence, what do we do. A knowledge of how Python numbers the position of each substring is therefore required.

How Python gives indices: Python gives the first letter in a string the position(index) 0 and the next 0+1 = 1 and the next 0+1+1=2 and it continues so. Since the first letter is not 1 that means our string's last member will have the index 24, this can also be accessed using the index -1. This means when counting from left to right, Python starts from 0 but when counting from right to left Python begins from -1,-2 till it gets to the first. Look at the image below:

 

 

Back to our problem, we want to get the first letter of the string called sentence. We use square brackets just after the string or the string's name and put the index of the letter we want between the square brackets:

print(sentence[0])

This should print out the first letter 'm' to the screen for you. So with that you can get any of the letters from the string, but what if you put in 25? Our string *ranges* from 0-24 so putting in 25 will mean you are asking for a letter that is not in the string and therefore out of the range. This will raise an error which we shall learn to handle. Another problem: What if we wanted to get 'mom' or any other word from the string. Here comes slicing, we specify the beginning index (that is, where we want Python to put the knife first), separated by a colon(:) and the last index (not inclusive), that is the border. All this should happen in the square brackets. So to get 'mom' printed out from 'my mom is going to school': The first 'm' stands at index 3 and the last 'm' stands at index 5, but we want it to be included so we give the next index as the border (which is 6).

print(sentence[3:6])
This should print out
>> mom
STRING OPERATIONS

If you want Python to begin slicing the list from the beginning, you do not need to begin with 0, you can begin like this:

print(sentence[ :6]))

This should print out:
>> my mom

The same way if you want Python to begin from somewhere say after 'mom' to the very last letter, then you can do this:

print(sentence[6: ])

This should give you:
>> is going to school

Skipping some sub strings You can also specify steps at which Python should slice a list using a third index called the step. So generally, to slice a string:

ListName[begin:end:step]

print(sentence[ : : 2])

This means Python should print the first letter, jump to the next 2 and print that and jump to the next 2 and should continue to do so until it gets to the end. This will be very useful when working with lists like a list off counting numbers and you want to get only even numbers or odd numbers.

STRING METHODS

Strings can further be manipulated using mathematical operations and built-in method. You can attach two strings together by using the + operator:

'my mom is '+'going to school'
will produce (Please note the space at the end of 'is'):
>> my mom is going to school

You can also repeat a string using the * operator:
'my mom ' * 3
Should produce:
>> my mom my mom my mom

To find out if a character or characters occur in a string, you could use *in* or *not in* keywords:

This will be better understood under conditioning your code section. But for now:


if 'm' in sentence:
    print("m exists")
else:
    print("nope, no m")
    

 

Python also provides built-in methods that can be used to manipulate strings in other ways. These methods are called on the string in question and if arguments are required for the method to properly execute, you provide those arguments. You must call the string and then bring a dot(.) and the name of the method after which comes parenthesis or round brackets in which arguments are provided if any. The parenthesis are still brought even if there were no arguments to provide.

1. .capitalize()
This will capitalize the first letter of the string.

2. .center(width,filchar)
Returns a space-padded string with the original
string centered to a total of width given.

3. .count(substring,begin=0,end=len(string))
This counts how many times a sub-string occurs within the original string.
You can specify where Python should start looking from
using the begin argument and the end, where Python should stop looking.
These two however default to begin being the start and end being the end
of the string.


4	.decode(encoding='UTF-8',errors='strict')
Decodes the string using the codec registered for encoding.
encoding defaults to the default string encoding.

5	.encode(encoding='UTF-8',errors='strict')
Returns encoded string version of string; on error, default is to raise a
ValueError unless errors is given with 'ignore' or 'replace'.

6	.endswith(suffix, beg=0, end=len(string))
Determines if string or a substring of string (if starting
index beg and ending index end are given) ends with suffix;
returns true if so and false otherwise

7	.expandtabs(tabsize=8)
Expands tabs in string to multiple spaces;
defaults to 8 spaces per tab if tabsize not provided

8	.find(str, beg=0 end=len(string))
Determine if str occurs in string or in a sub-string
of string if starting index beg and ending index end
are given returns index if found and -1 otherwise

9	.index(str, beg=0, end=len(string))
Same as find(), but raises an exception if str not found

10	.isalnum()
Returns true if string has at least 1 character and
all characters are alphanumeric and false otherwise
11	.isalpha()
Returns true if string has at least 1 character and
all characters are alphabetic and false otherwise

12	.isdigit()
Returns true if string contains only
digits and false otherwise

13	.islower()
Returns true if string has at least 1 cased
character and all cased characters are in lowercase and false otherwise

14	.isnumeric()
Returns true if a unicode string contains only
numeric characters and false otherwise

15	.isspace()
Returns true if string contains only whitespace
characters and false otherwise

16	.istitle()
Returns true if string is properly "title-cased" and false otherwise

17	.isupper()
Returns true if string has at least one cased
character and all cased characters are
in uppercase and false otherwise

18	.join(seq)
Merges (concatenates) the string representations of elements
in sequence seq into a string, with separator string

19	.len(string)
Returns the length of the string

20	.ljust(width[, fillchar])
Returns a space-padded string with the original
string left-justified to a total of width columns
21	.lower()
Converts all uppercase letters in string to lowercase

22	.lstrip()
Removes all leading white space in string

23	.maketrans()
Returns a translation table to be used in translate function.

24	.max(str)
Returns the max alphabetical character from the string str

25	.min(str)
Returns the min alphabetical character from the string str

26	.replace(old, new [, max])
Replaces all occurrences of old in string with new or
at most max occurrences if max given

27	.rfind(str, beg=0,end=len(string))
Same as find(), but search backwards in string

28	.rindex( str, beg=0, end=len(string))
Same as index(), but search backwards in string

29	.rjust(width,[, fillchar])
Returns a space-padded string with the original string
right-justified to a total of width columns.

30	.rstrip()
Removes all trailing whitespace of string
31	.split(str="", num=string.count(str))
Splits string according to delimiter str
(space if not provided) and returns list of
substrings; split into at most num substrings if given

32	.splitlines( num=string.count('\n'))
Splits string at all (or num) NEWLINEs and
returns a list of each line with NEWLINEs removed

33	.startswith(str, beg=0,end=len(string))
Determines if string or a substring of string
(if starting index beg and ending index end are given)
starts with substring str; returns true
if so and false otherwise

34	.strip([chars])
Performs both lstrip() and rstrip() on string

35	.swapcase()
Inverts case for all letters in string

36	.title()
Returns "titlecased" version of string, that is, all
words begin with uppercase and the rest are lowercase

37	.translate(table, deletechars="")
Translates string according to translation
table str(256 chars), removing
those in the del string

38	.upper()
Converts lowercase letters in string to uppercase

39	.zfill (width)
Returns original string left-padded with zeros
to a total of width characters; intended for numbers,
zfill() retains any sign given (less one zero)

40	.isdecimal()
Returns true if a unicode string contains
only decimal characters and false otherwise
String Formatting

One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C's printf() family. Following is a simple example:

print ("My name is %s and weight is %d kg!") % ('Ellie', 60)

When the above code is executed, it produces the following result:

>>My name is Ellie and weight is 60 kg!

Here is the list of complete set of symbols which can be used along with %:

Symbol	Functionality

*	argument specifies width or precision

-	left justification

+	display the sign

	leave a blank space before a positive number

#	add the octal leading zero ( '0' ) or hexadecimal
leading '0x' or '0X', depending on whether 'x' or 'X' were used.
0	pad from left with zeros (instead of spaces)

%	'%%' leaves you with a single literal '%'

(var)	mapping variable (dictionary arguments)

m.n.	m is the minimum total width and n is the number
of digits to display after the decimal point (if appl.)
TRIPLE QUOTES

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters. The syntax for triple quotes consists of three consecutive single or double quotes.


para_str = """this is a long string that is made up of
several lines and non-printable characters such as
TAB ( \t ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [ \n ], or just a NEWLINE within
the variable assignment will also show up.
"""
print (para_str)
When the above code is executed, it produces the following result.
Note how every single special character has been converted to its printed form,
right down to the last NEWLINE at the end of the string between the "up."
and closing triple quotes.

Also note that NEWLINEs occur either with an explicit carriage return
at the end of a line or its escape code (\n):

this is a long string that is made up of
several lines and non-printable characters such as
TAB (    ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [
 ], or just a NEWLINE within
the variable assignment will also show up.
Raw strings don't treat the backslash as a special character at all.
Every character you put into a raw string stays the way you wrote it:



print ('C:\\nowhere')
When the above code is executed, it produces the following result:

>>C:\nowhere

Now let's make use of raw string.
We would put expression in r'expression' as follows:



print (r'C:\\nowhere')
When the above code is executed, it produces the following result:

>>C:\\nowhere
UNICODE STRINGS

Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world. I'll restrict my treatment of Unicode strings to the following:

print (u'Hello, world!')
When the above code is executed, it produces the following result:

>>Hello, world!

As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.

Take Test

Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object. It means numbers when created cannot be altered, an alteration due to an operation on or a manipulation of the number only creates a new number. To create a number in Python simply assign a value to a name. Number objects are created when you assign a value to them. For example:

 
>> var1 = 1

>> var2 = 10

You can also delete the reference to a number object by using the del statement.
The syntax of the del statement is:

>> del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement.
For example:

>> del var
>> del var_a, var_b

 

Python Numerical Types

Python supports four different numerical types: int (signed integers): often called just integers or ints, are positive or negative whole numbers with no decimal point. long (long integers ): or longs are integers of unlimited size, written like integers and followed by an uppercase or lowercase L. float (floating point real values) : or floats, represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250). complex (complex numbers) : are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). a is the real part of the number, and b is the imaginary part. Complex numbers are not used much in Python programming. Examples: Here are some examples of numbers:

 
int
long
float
complex
10
51924361L
0.0
3.14j
100
-0x19323L
15.20
45.j
-786
0122L
-21.9
9.322e-36j
080
0xDEFABCECBDAECBFBAEL

32.3
.876j
-0490
535633629843L
-90.
-.6545+0J
-0x260
-052318172735L
-32.54e100
3e+26J
0x69
-4721885298529L
70.2-E12
4.53e-7j

Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L. A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number.

Number Type Conversion

Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But sometimes, you'll need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter. In simple terms, when you are doing calculation involving numbers in Python, Python will convert some number types to others for a uniform calculation but in other cases, you will have to do the conversion yourself. The language therefore provides the following ways to convert from one type to another:

Type int(x)to convert x to a plain integer.

Type long(x) to convert x to a long integer.

Type float(x) to convert x to a floating-point number.

Type complex(x) to convert x to a complex number with real part x and imaginary part zero.

Type complex(x, y) to convert x and y to a complex number with
real part x and imaginary part y. x and y are numeric expressions
Mathematical Functions

Python includes the following functions that perform mathematical calculations:

Function	Returns ( description )

abs(x)	The absolute value of x: the (positive) distance between x and zero.

ceil(x)	The ceiling of x: the smallest integer not less than x

cmp(x, y)	-1 if x < y, 0 if x == y, or 1 if x > y

exp(x)	The exponential of x: ex

fabs(x)	The absolute value of x.

floor(x)	The floor of x: the largest integer not greater than x

log(x)	The natural logarithm of x, for x> 0

log10(x)	The base-10 logarithm of x for x> 0 .

max(x1, x2,...)	The largest of its arguments: the value closest to positive infinity

min(x1, x2,...)	The smallest of its arguments: the value closest to negative infinity

modf(x)	The fractional and integer parts of x in a two-item tuple.
Both parts have the same sign as x. The integer part is returned as a float.

pow(x, y)	The value of x**y.

round(x [,n])	x rounded to n digits from the decimal point.
Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.

sqrt(x)	The square root of x for x > 0

 

Random Number Functions

Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions that are commonly used.

Function	Description
choice(seq)	A random item from a list, tuple, or string.

randrange ([start,] stop [,step])	A randomly selected element from range(start, stop, step)

random()	A random float r, such that 0 is less than or equal to r and r is less than 1

seed([x])	Sets the integer starting value used in generating random numbers.
Call this function before calling any other random module function. Returns None.

shuffle(lst)	Randomizes the items of a list in place. Returns None.

uniform(x, y)	A random float r, such that x is less than or equal to r and r is less than y

 

Trigonometric Functions

Python includes following functions that perform trigonometric calculations:

Function
Description
acos(x)
Return the arc cosine of x, in radians
asin(x)
Return the arc sine of x, in radians.
atan(x)
Return the arc tangent of x, in radians.
atan2(y, x)
Return atan(y / x), in radians.
cos(x)
Return the cosine of x radians.v
hypot(x, y)
Return the Euclidean norm, sqrt(x*x + y*y).
sin(x)
Return the sine of x radians.
tan(x)
Return the tangent of x radians.
degrees(x)
Converts angle x from radians to degrees.
radians(x)
Converts angle x from degrees to radians.
 
Mathematical Constants

The module also defines two mathematical constants:

Constants
Description
pi
The mathematical constant pi.
piThe mathematical constant pi.
 e
The mathematical constant e.

 

 

What is an operator?

Simple answer can be given using expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + is called operator. Python language supports the following types of operators.

  • Arithmetic Operators
  • Comparison (i.e., Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Let's have a look on all operators one by one.

Python Arithmetic Operators

Assume variable a holds 10 and variable b holds 20, then:

    a = 10
    b = 20
 Operator
Description
Example
+   Addition 
Adds values on either side of the operator   
a + b will give 30
-  Subtraction
Subtracts right hand operand from left hand operand
a - b will give -10
*  Multiplication
Multiplies values on either side of the operator
a * b will give 200
/  Division
Divides left hand operand by right hand operand
b / a will give 2
%  Modulo
Divides left hand operand by right hand operand and returns remainder
b % a will give 0
** Exponent
Performs exponential (power) calculation on operators
a**b will give 10 to the power 20
//  Floor Division
The division of operands where the result is the quotient in which the digits after the decimal point are removed.
9//2 is equal to 4 and
9.0//2.0 is equal to 4.0

Python Comparison Operators

Assume variable a holds 10 and variable b holds 20, then:

    a = 10
    b = 20

 

Operator
Description
Example
== (it is equal to)
Checks if the value of two operands are equal or not, if yes then condition becomes true.
(a == b) is false.
!= (it is not equal to)
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
(a != b) is true.
< >
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.  This is similar to != operator.
(a <> b) is true.
> (greater than)
Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true.
(a > b) false.
 < (less than)
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(a < b) is true.
 >= (greater than or equal to)
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(a >= b) is false.
<= (less than or equal to)
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.  
(a <= b) is true.
    a = 10
    b = 20

Python Assignment Operators

Assume variable a holds 10 and variable b holds 20, then:

    a = 10
    b = 20

 

Operator
 Description
Example
= (equals)
Simple assignment operator, Assigns values from right side operands to left side operand
c = a + b will assign the value of a + b to c
+=
Add AND assignment operator, It adds right operand to the left
operand and assign the result to left operand
c += a is equivalent to c = c + a
-=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand  
c -= a is equivalent to c = c - a
 
*=
Multiply AND assignment operator, It multiplies right operand with
the left operand and assign the result to left operand
c *= a is equivalent to c = c * a
/=
Divide AND assignment operator, It divides left operand with the
right operand and assign the result to left operand
c /= a is equivalent to c = c / a
 %=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
c %= a is equivalent to c = c % a
**=
Exponent AND assignment operator,  Performs exponential (power) calculation on operators and assign value to the left operand  
c **= a is equivalent to c = c ** a
//=
Floor Division and assigns a value, Performs floor division on operators and assign value to the left operand  
c //= a is equivalent to c = c // a

Python Bit-wise Operators

Bitwise operator works on bits and perform bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows:

a = 0011 1100
b = 0000 1101
-----------------
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011
    

There are following Bit-wise operators supported by Python language

 

 Operator
Description
Example
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(a & b) will give 12 which is 0000 1100
|
Binary OR Operator copies
a bit if it exists in either operand.
(a | b) will give 61 which is 0011 1101
^
 Binary XOR Operator copies the bit if it is set in one operand but not both.  
(a ^ b) will give 49 which is 0011 0001
~
Binary Ones Complement Operator is unary and has the effect of
'flipping' bits.
(~a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<<
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
a << 2 will give 240 which is 1111 0000
>>
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.  
a >> 2 will give 15 which is 0000 1111

 

Python Logical Operators

There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then:

    a = 10
    b = 20
Operator
Description
 Example
 and
Called Logical AND operator. If both the operands are true then then condition becomes true.    
(a and b) is true. Meaning, a and b are non zero. They both exist
or
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true.
 
not
Called Logical NOT Operator. Used to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
not(a and b) is false.

Python Membership Operators

In addition to the operators discussed previously, Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators explained below:

y = [1,'x',2,'hello','Effie',5.6]

 

Operator
Description
Example
in
Evaluates to true if it finds a variable in the specified sequence and false otherwise.
'x' in y, here it results in a 1(true) if x is a member of sequence y. And from above sequence x exists.
not in
Evaluates to true if it does not finds a variable in the specified
sequence and false otherwise.
'Effie' not in y will result in false since 'Effie' exists in the above sequence

 

Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator
Description
Example
is
Evaluates to true if the variables on either side of the operator  point to the same object and false otherwise.
x is y, here it results in 1(true) if id(x) equals id(y).
is not
Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
x is not y, here it does not results in 1(true) if id(x) is not equal to id(y).

 

Python Operators Precedence

The following table lists all operators from highest precedence to lowest. This is the order of operation (The 'BODMAS') of Python.

 

Operator
Description
**
Exponentiation (raise to the power)
~ + -
complement, unary plus and minus
(method names for the last two are +@ and -@)
 * / % //
Multiply, divide, modulo and floor division
+ -
Addition and subtraction
>> <<
Right and left bitwise shift
&
Bitwise 'AND'
^ |
Bitwise exclusive `OR' and regular `OR'
<= < > >= 
Comparison operators
<> == !=
Equality operators
= %= /= //= -= += *= **=
Assignment operators
is is not
Identity operators
in not in
Membership operators
not or and
Logical operators

 

TIP
You do not have to keep all these rules in your memory, you will definitely get used to them as you write your own programs. But keep these rules somewhere as you will have to keep getting back to them as the need arises

 

Take Test

 

What are Boolean Values?

Python uses bool-ean value to express true or false values. Boolean values are only two, True and False Boolean value is named after a British Mathematician called George Boole, George Boole created Boolean Algebra, which is the basis of all modern computer arithmetic. In Python, boolean values are True and False, take notice of the capitalization because true and false are not boolean values and therefore will throw an error. The boolean values True and False are not quoted like strings, which means Python recognises them written raw like that.

Boolean Expressions

These are Python expressions or "calculations" that return a boolean value (True or False). Expressions that use comparison operators return boolean value as a result. For example, If this were put to you: 3 is greater than 2, your response will not be a number like 5 because we are comparing. You will probably say: "That's not true (False)". But an expression like: 3 + 2 should nt yield a true or false answer but another number. So basically, when you write a boolean expression, you should expect True or False and based on the boolean value returned, you can continue your program or stop or branch.

TIP
if statements are boolean expressions also, because they check if a condition is True or False. If statements however, might not return a True or False value but will execute the code under the "if condition" if the condition evaluates to true and will execute the code under the "else condition" if the condition is false. Loops also rely on boolean values to execute. In fact, conditional expressions depend on boolean values to execute.

Examples

Copy and run the following code, it might help in understanding the boolean concept

    print(type(True))
    print(type("False"))
    print(3 > 2)
    print(5==5)
    print(6>=6)
    if 5 > 6:
    #what gets printed should the above expression be True
        print("Yes, 5 is greater")
    #What gets printed when the above expression is False
    else:
        print("Nope, 5 is not greater")

 

Take Test

Python's list is another datatype that can be used to keep other basic datatypes in a fairly organised manner. This is because members of a list are given position numbers by the Python interpreter and therefore can be accessed using their positions called indexes. A list is therefore a sequence.

To define a python list:

listname = [first_member,second_member,...]

These members can be any of the following datatypes:

Boolean
Number
String
List
Tuple
Dictionary

 

List Indexing

Python assigns integers to the position of the members in the list. The first_member gets the index 0, the second_member gets the index 1 and so on Python adds 1 to the previous index to get the index of the current member.Since a list can contain a lot of members at once, the last member is referenced -1 to make it easier to access.

Indexing Vs. Slicing Lists

When you create a list in python, python gives a position number to each item in the list depending on where it appears in the list, the numbering is done from 0 to the last item (n-1), where n is the number of items in the list. So, if there are 10 items in a list, the first item will take position(index) 0 and the last item will take (10-1) = 9.

You can therefore request Python to give you a particular item from the list by specifying it's index to Python - this is called indexing.

You can also ask Python to give you items from a certain point to a certain point, you can even specify if Python should jump over particular ones - this is called list slicing.

The Program

My_list = ['hello', 2,3.6,True]

#Let's call print
print(My_list)

#Now let's specify some index
print(My_list[3])

#This should print the
#fourth item in the
#list which is at index 3.

#Now let's slice

print(My_list[0:3])

#This should print from the first
#item to the third item,
#leaving out the one at
#index 3

 

When you want Python to print from the beginning of the list, you don't need to specify the 0, just do this:

list_name [ : end index here].

If you want Python to print out every member of the list but at a certain step, then do this:

list_name[ : :step]

If your step was 2, then Python will print the first item and jump to the third and jump to the fifth. That's a smart way to print out odd numbers if your list is made of counting numbers.

You can simply ask for a reverse list by making your step -1, this will tell Pytjon to print the last item first and go backwards. Like this:

list_name[ : : -1]

Python should print out a reverse list.

Try it Out
Create a list of counting numbers from 0 to 21, extract a reverse of even numbers from it. Python should print out a reverse list.

Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

list = ['physics', 'chemistry', 1997, 2000];

print ("Value available at index 2 : ",list[2])

list[2] = 2001
print ("New value available at index 2 : ",list[2])

When the above code is executed, it produces the following result:

Value available at index 2 : 1997
New value available at index 2 : 2001

Delete List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. Following is a simple example:

list1 = ['physics', 'chemistry', 1997, 2000];

>> print (list1)
>> del list1[2]
print ("After deleting value at index 2 : ",list1;)

When the above code is executed, it produces following result:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

 

Basic List Operations

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string. In fact, lists respond to all of the general sequence operations we used on strings in the Strings section.

Python Expression
Results
Description
len([1, 2, 3])
3
Length
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
Concatenation
['Hi!'] * 4
['Hi!', 'Hi!', 'Hi!', 'Hi!']
Repetition
3 in [1, 2, 3]
True
Membership

for x in [1, 2, 3]:

    print x

1

2

3

Iteration

 

Indexing, Slicing, and Matrices

Because lists are sequences, indexing and slicing work the same way for lists as they do for strings. Assuming following the input:

L = ['spam', 'Spam', 'SPAM!']

Python Expression
Results
Description
L[2]
'SPAM!'
Offsets start at zero
L[-2]
'Spam'
Negative: count from the right
L[1:]
['Spam', 'SPAM!']
Slicing fetches sections

Built-in List Functions & Methods

Python includes the following list functions:

SN	Function with Description

1	cmp(list1, list2)
Compares elements of both lists.

2	len(list)
Gives the total length of the list.

3	max(list)
Returns item from the list with max value.

4	min(list)
Returns item from the list with min value.

5	list(seq)
Converts a tuple into list.
Python includes following list methods

SN	Methods with Description
1	list.append(obj)
Appends object obj to list

2	list.count(obj)
Returns count of how many times obj occurs in list

3	list.extend(seq)
Appends the contents of seq to list

4	list.index(obj)
Returns the lowest index in list that obj appears

5	list.insert(index, obj)
Inserts object obj into list at offset index

6	list.pop(obj=list[-1])
Removes and returns last object or obj from list

7	list.remove(obj)
Removes object obj from list

8	list.reverse()
Reverses objects of list in place

9	list.sort([func])
Sorts objects of list, use compare func if given

 

Take Test

There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially(From top to down): The first statement in a function is executed first, followed by the second, and so on. But conditioning your code can make Python behave in another way while executing the code from top to down. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times.

Python programming language provides the following types of loops to handle looping requirements.

Loop Type
Description
while loop
Repeats a statement or group of statements while a given condition is true.
   It tests the condition before executing the loop body.
for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops
You can use one or more loop inside any another while, for or do..while loop.
Loop Control
 Statements
Loop control statements change execution from its normal sequence.
  When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
 
 

 

Python supports the following control statements.

 

Control Statement
Description
break statement
Terminates the loop statement and transfers
          execution to the statement immediately following the loop.
continue statement
Causes the loop to skip the remainder of its body and
                                 immediately retest its condition prior to reiterating.
pass statement
The pass statement in Python is used when a statement is
required syntactically but you do not want any command
                                 or code to execute.

Take Test

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Explaining Functions

Functions are defined as a block of reusable code. It means we want a way to store our code that does a particular activity so that just as we call a variable after storing a value in it, we can simply call the block of code by name and the activity will be done. 

Those of you who have a math background will notice that functions are from algebra. Imagine you want to calculate the area of a triangle. 

Every time you are given the dimensions of the triangle, you write down a formula and then plug in the dimensions and then you have your answer.

 Your formula acts as the function or a magic box, plug in the values and then you have your answer but the difference between a formula and a function is that, with formula, you would have to solve after plugging in the values, but with a function the answer can be deduced just after plugging in the values because it is solved once, it's not much of a difference though. 

Let's see area of a triangle. 

Area = 0.5*base*height
So if you were given the base and height to be 15 and 5 respectively, then you have: Area = 0.5*15*5 You just need to simplify this.

 Now supposed you were given the same type of questions with different dimensions, about 10 questions. 

Every time you need to write the formula and plug in the values for all the 10 questions. This is where functions come in handy, you define the formula once and anytime you call the function and plug in your value, you get your answer. 

Defining functions

  1. Since we need to call the function, that means we need to give it a name. 
  2. We need to plug in values with certain formulas, so if needed then we have to make space for where we will plug in the values, this is called parameters or function input 
  3.  Now we define what the function does. For example in area of a triangle, the function multiplies 0.5 with the input called base and the input called height 
  4. Now what answer do we want our function to return? This is called the return value. In the example above, we would like the function to return the final answer, that is the Area. 

Note: In Python, you can choose not to return anything.

 Python begins a function with "def" 

# The program:

def area_of_tria(base,height):
    Area = 0.5*base*height
    return Area

# Now to call our function and print our function's return value

print (area_of_tria(15,5))

# End of program. 

Syntax: 

  1. Begin a function with "def" and provide the function name, choosing a name that easy to associate with what the function does is appropriate. 
  2. Bring parentheses after the name of the function and if your function takes inputs then specify the inputs it takes in the parentheses. If it does not take any input, still bring the brackets but don't specify anything in it. 
  3. Bring a colon after the brackets 
  4. What the function does follows, this should be on the next line and should be evenly indented (usually four spaces or a tab)
  5. Now bring the keyword "return" and bring what you want the function to return after it. 
  6. To call a function, write out the name of the function and provide inputs in the brackets if it requires inputs. 
  7. To see the return value of the function, please bring the function print() before calling your own function.
Try yourself

Write a function that takes length as input and returns the perimeter of a square.

 

Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

  • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or
  • The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax

def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]
    

By default, parameters have a positional behavior and you need to inform them in the same order that they were defined. Example: Here is the simplest form of a Python function. This function takes a string as input parameter and prints it on standard screen.

def printme( str ):
   "This prints a passed string into this function"
   print str
   return
    

Calling a Function

Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function:

# Function definition is here
def printme( str_par ):
   "This prints a passed string into this function"
   print str_par
   return
# Now you can call printme function
printme("I'm the first call to user defined function!")
printme("Again second call to the same function")
    

When the above code is executed, it produces the following result:

I'm first call to user defined function!
Again second call to the same function
Pass by reference vs value
    

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example:

# Function definition is here
def changeme( mylist ):
   "This changes a passed list into this function"
   mylist.append([1,2,3,4])
   print ("Values inside the function: ", mylist)
   return

# Now you can call changeme function
mylist = [10,20,30]
changeme( mylist )
print ("Values outside the function: ", mylist)
    

Here, we are maintaining reference of the passed object and appending values in the same object. So, this would produce the following result:

Values inside the function:  [10, 20, 30, [1, 2, 3, 4]]
Values outside the function:  [10, 20, 30, [1, 2, 3, 4]]
    

There is one more example where argument is being passed by reference and the reference is being overwritten inside the called function.

# Function definition is here
def changeme( mylist ):
   "This changes a passed list into this function"
   mylist = [1,2,3,4];  # This would assig new reference in mylist
   print ("Values inside the function: ", mylist)
   return

# Now you can call changeme function
mylist = [10,20,30]
changeme( mylist )
print ("Values outside the function: ", mylist)
    

The parameter mylist is local to the function changeme. Changing mylist within the function does not affect mylist. The function accomplishes nothing and finally this would produce the following result:

Values inside the function:  [1, 2, 3, 4]
Values outside the function:  [10, 20, 30]
    

Function Arguments

You can call a function by using the following types of formal arguments:

  • Required arguments
  • Keyword arguments
  • Default arguments
  • Variable-length arguments

Required arguments

Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. To call the function printme(), you definitely need to pass one argument, otherwise it would give a syntax error as follows:

# Function definition is here
def printme( str ):
   "This prints a passed string into this function"
   print str
   return

# Now you can call printme function
printme();
When the above code is executed, it produces the following result:
Traceback (most recent call last):
  File "test.py", line 11, in 
    printme()
TypeError: printme() takes exactly 1 argument (0 given)
    

Keyword arguments

Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. You can also make keyword calls to the printme() function in the following ways:

# Function definition is here
def printme( str ):
   "This prints a passed string into this function"
   print (str)
   return

# Now you can call printme function
printme( str = "My string");
When the above code is executed, it produces the following result:
My string
    

Following example gives more clear picture. Note, here order of the parameter does not matter.

# Function definition is here
def printinfo( name, age ):
   "This prints a passed info into this function"
   print ("Name: ", name)
   print ("Age ", age)
   return

# Now you can call printinfo function
printinfo( age=50, name="miki" )
When the above code is executed, it produces the following result:
Name:  miki
Age  50
    

Default arguments

A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. Following example gives an idea on default arguments, it would print default age if it is not passed:

# Function definition is here
def printinfo( name, age = 35 ):
   "This prints a passed info into this function"
   print ("Name: ", name)
   print ("Age ", age)
   return
# Now you can call printinfo function
printinfo( age=50, name="miki" );
printinfo( name="miki" );
When the above code is executed, it produces the following result:
Name:  miki
Age  50
Name:  miki
Age  35
    

Variable-length arguments

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments. The general syntax for a function with non-keyword variable arguments is this:

def functionname([formal_args,] *var_args_tuple ):
   "function_docstring"
   function_suite
   return [expression]
    

An asterisk (*) is placed before the variable name that will hold the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. Following is a simple example:

# Function definition is here
def printinfo( arg1, *vartuple ):
   "This prints a variable passed arguments"
   print ("Output is: ",arg1)

   for var in vartuple:
      print (var)
   return
# Now you can call printinfo function
printinfo( 10 )
printinfo( 70, 60, 50 )
    

When the above code is executed, it produces the following result:

Output is:
10
Output is:
70
60
50
    

The Anonymous Functions (Lambda expressions)

You can use the lambda keyword to create small anonymous functions. These functions are called anonymous because they are not declared in the standard manner by using the def keyword.

  • Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.
  • An anonymous function cannot be a direct call to print because lambda requires an expression.
  • Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.
  • Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons.

Syntax

The syntax of lambda functions contains only a single statement, which is as follows:

lambda [arg1 [,arg2,.....argn]]:expression

Following is the example to show how lambdaform of function works:

# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2
 
# Now you can call sum as a function
print ("Value of total : ", sum( 10, 20 ))
print ("Value of total : ", sum( 20, 20 ))
When the above code is executed, it produces the following result:
Value of total :  30
Value of total :  40
    

The return Statement

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. All the above examples are not returning any value, but if you like you can return a value from a function as follows:

# Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2
   print ("Inside the function : ", total)
   return total;
# Now you can call sum function
total = sum( 10, 20 )
print ("Outside the function : ", total)
When the above code is executed, it produces the following result:
Inside the function :  30
Outside the function :  30
    

Scope of Variables

All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable. The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python:

  • Global variables
  • Local variables

Global vs. Local variables

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope. Following is a simple example:

total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print ("Inside the function local total : ", total)
   return total
# Now you can call sum function
sum( 10, 20 )
print ("Outside the function global total : ", total)
When the above code is executed, it produces the following result:
Inside the function local total :  30
Outside the function global total :  0
 

 

Take Test

Most of our daily activities are based on conditions. "I will go to the grocery when or if the rain stops", "This team can only win the world cup if it beats the other team in the finals." Sometimes, the conditions maybe more than one; "this team will win the world cup if it wins in the semi finals AND in the finals." Other times the condition maybe either this or that; "Nigeria will be through to the next stage if they win OR draw their next match." In each case, something happens when then condition given occurs (becomes true); "this team will win the world cup" and another thing happens when the condition given does not occur (becomes false);"this team will not win the world cup".

if,elif and else

Computer programming allows for specifying conditions that will be checked before executing a particular code. Python uses  if, elif ,else  to check for conditions before executing a particular code. You can therefore place your code under a certain condition which is to be checked by the interpreter before executing the code.

The Syntax

  1. Bring the beginning condition clause (if, elif, else) and then place the condition to be evaluated just a space after the if or elif (else does not take a condition)
  2. Use comparison operators (<, >, ==,..) when neccessary
  3. If there are more than one conditions to be evaluated, then bring the appropriate logical operator (and/or)
  4. Bring a colon(:) and then move to the next line and tab (or move four spaces to the right)
  5. Specify what should happen is if the condition becomes true
  6. use else or elif to specify what should happen if the previous condition is false

The following example should set the pace: This programs checks the current value of temperature named temp and tells the user what the weather outside should be. Feel free to copy and paste the code in your editor. Play around it.(The hash (#) is used to comment in Python code: The interpreter therefore ignores words that come after a #.

    #Takes temperature value as input from user
    temp = input("Enter the current temperature value:")
    #Converts and updates temp value from string to an integer
    temp = int(temp)
    #checks if temp is greater than 30
    if temp > 30:
        print("Its hot outside")
    #checks if temp is equal to 30
    elif temp == 30:
        print("It should be a normal weather")
    #checks if temp is less than 30 AND greater than 15
    #Both must be true for the code (print("The weather is cold"))
    #To execute
    elif temp < 30 and temp > 15:
        print ("The weather is cold")
    else:
        print("it's freezing out there")

Nested Conditions

These are conditions that depend on other conditions to evaluate their own statements. Consider this: Go to my house (number 13) and check if my Dad or Mom is home and then give the money to my Dad. but if my Dad is not home, you could give it to my mom. if none of the two is home, bring back my money to me. And return my money if you do not find my house. You realize that you would have by passed a lot of houses on your way to my house because they do not bear the number 13. This condition has to be first fulfilled before you look for my dad or mom and give out the money. So giving out the money to my dad or mom depends on whether you found my house or not. Let us write a simple Python program to illustrate this idea:

    #Takes house number as input and converts to integer on the same line
    house = int(input("Enter house number"))

    #First condition to check if house number is 13
    if house == 13:
        #After house number is 13, checks if dad or mom is home
        #Takes Yes or No as input representing the presence of dad or mom
        dad_or_mom = input("Is Dad or Mom Home: Yes/No:")
        if dad_or_mom == 'Yes':
            print("Give this money to him or her")
        #If mom or dad is not home
        else:
            print("I found the house but neither dad nor mom was home")
    #If house number is not 13
    else:
        print("I could not find the house")

 

After running and trying out a few inputs, you will notice that the option that asks if dad or mom is home is never shown except if the house number is 13. This means that you do not ask the question "Is mom or dad home?" if you have not found the house you were sent to (number 13).

TIP
Remember to always make your program as practical as possible, this helps you understand how the computer "thinks" while running your code and if you understand how the computer "thinks" then you can manipulate it as much as possible.

 

Take Test

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Example: The Python code for a module named anamenormally resides in a file named aname.py. Here's an example of a simple module, support.py

def print_func( par ):
   print ("Hello : ", par)
   return

The import Statement

You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax:

import module1[, module2[,... moduleN]

When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module hello.py, you need to put the following command at the top of the script:

# Import module support
import support

# Now you can call defined function that module as follows
support.print_func("Effie")

When the above code is executed, it produces the following result:
Hello : Effie

 

A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.

The from...import Statement

Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax:

from modname import name1[, name2[, ... nameN]]
    

For example, to import the function fibonacci from the module fib, use the following statement:

from fib import fibonacci
    

This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module.

The from...import * Statement

It is also possible to import all names from a module into the current namespace by using the following import statement:

from modname import *
    

This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences:

  1. The current directory.
  2. If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
  3. If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.

The PYTHONPATH Variable

The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH. Here is a typical PYTHONPATH from a Windows system:

set PYTHONPATH=c:\python20\lib;
And here is a typical PYTHONPATH from a UNIX system:
set PYTHONPATH=/usr/local/lib/python
    

Namespaces and Scoping

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values). A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable. Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions. Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local. Therefore, in order to assign a value to a global variable within a function, you must first use the global statement. The statement global VarName tells Python that VarName is a global variable. Python stops searching the local namespace for the variable. For example, we define a variable Money in the global namespace. Within the function Money, we assign Money a value, therefore Python assumes Money as a local variable. However, we accessed the value of the local variable Moneybefore setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes the problem.

Money = 2000
def AddMoney():
   # Uncomment the following line to fix the code:
   # global Money
   Money = Money + 1

print (Money)
AddMoney()
print (Money)
    

The dir( ) Function: The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables and functions that are defined in a module. Following is a simple example: #!/usr/bin/python

# Import built-in module math
import math

content = dir(math)

print (content)

When the above code is executed, it produces the following result:\

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',
'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Here, the special string variable __name__  is the module's name, and __file__ is the filename from which the module was loaded.

The globals() and locals()Functions

The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called. If locals() is called from within a function, it will return all the names that can be accessed locally from that function. If globals() is called from within a function, it will return all the names that can be accessed globally from that function. The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.

The reload() Function

When the module is imported into a script, the code in the top-level portion of a module is executed only once. Therefore, if you want to reexecute the top-level code in a module, you can use the reload()function. The reload() function imports a previously imported module again. The syntax of the reload() function is this:

reload(module_name)

Here, module_name is the name of the module you want to reload and not the string containing the module name. For example, to reload hellomodule, do the following:

reload(hello)

Packages in Python

A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on. Consider a file Pots.py available in Phonedirectory. This file has following line of source code:

def Pots():
   print ("I'm Pots Phone")
    

Similar way, we have another two files having different functions with the same name as above:

  1. Phone/Isdn.py file having function Isdn()
  2. Phone/G3.py file having function G3()

Now, create one more file __init__.py in Phonedirectory:

  1. Phone/__init__.py

To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows:

from Pots import Pots
from Isdn import Isdn
from G3 import G3

After you've added these lines to __init__.py, you have all of these classes available when you've imported the Phone package.

# Now import your Phone Package.
import Phone

Phone.Pots()
Phone.Isdn()
Phone.G3()
When the above code is executed, it produces the following result:
I'm Pots Phone
I'm 3G Phone
I'm ISDN Phone
NOTE
In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. You can also define different Python classes in those files and then you can create your packages out of those classes.

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code. Example: The Python code for a module named anamenormally resides in a file named aname.py. Here's an example of a simple module, support.py

def print_func( par ):
   print ("Hello : ", par)
   return

The import Statement

You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax:

import module1[, module2[,... moduleN]

When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module hello.py, you need to put the following command at the top of the script:

# Import module support
import support

# Now you can call defined function that module as follows
support.print_func("Effie")

When the above code is executed, it produces the following result:
Hello : Effie

 

A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.

The from...import Statement

Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax:

from modname import name1[, name2[, ... nameN]]
    

For example, to import the function fibonacci from the module fib, use the following statement:

from fib import fibonacci
    

This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module.

The from...import * Statement

It is also possible to import all names from a module into the current namespace by using the following import statement:

from modname import *
    

This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences:

  1. The current directory.
  2. If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
  3. If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.

The PYTHONPATH Variable

The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH. Here is a typical PYTHONPATH from a Windows system:

set PYTHONPATH=c:\python20\lib;
And here is a typical PYTHONPATH from a UNIX system:
set PYTHONPATH=/usr/local/lib/python
    

Namespaces and Scoping

Variables are names (identifiers) that map to objects. A namespace is a dictionary of variable names (keys) and their corresponding objects (values). A Python statement can access variables in a local namespace and in the global namespace. If a local and a global variable have the same name, the local variable shadows the global variable. Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions. Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local. Therefore, in order to assign a value to a global variable within a function, you must first use the global statement. The statement global VarName tells Python that VarName is a global variable. Python stops searching the local namespace for the variable. For example, we define a variable Money in the global namespace. Within the function Money, we assign Money a value, therefore Python assumes Money as a local variable. However, we accessed the value of the local variable Moneybefore setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes the problem.

Money = 2000
def AddMoney():
   # Uncomment the following line to fix the code:
   # global Money
   Money = Money + 1

print (Money)
AddMoney()
print (Money)
    

The dir( ) Function: The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables and functions that are defined in a module. Following is a simple example: #!/usr/bin/python

# Import built-in module math
import math

content = dir(math)

print (content)

When the above code is executed, it produces the following result:\

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',
'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Here, the special string variable __name__  is the module's name, and __file__ is the filename from which the module was loaded.

The globals() and locals()Functions

The globals() and locals() functions can be used to return the names in the global and local namespaces depending on the location from where they are called. If locals() is called from within a function, it will return all the names that can be accessed locally from that function. If globals() is called from within a function, it will return all the names that can be accessed globally from that function. The return type of both these functions is dictionary. Therefore, names can be extracted using the keys() function.

The reload() Function

When the module is imported into a script, the code in the top-level portion of a module is executed only once. Therefore, if you want to reexecute the top-level code in a module, you can use the reload()function. The reload() function imports a previously imported module again. The syntax of the reload() function is this:

reload(module_name)

Here, module_name is the name of the module you want to reload and not the string containing the module name. For example, to reload hellomodule, do the following:

reload(hello)

Packages in Python

A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on. Consider a file Pots.py available in Phonedirectory. This file has following line of source code:

def Pots():
   print ("I'm Pots Phone")
    

Similar way, we have another two files having different functions with the same name as above:

  1. Phone/Isdn.py file having function Isdn()
  2. Phone/G3.py file having function G3()

Now, create one more file __init__.py in Phonedirectory:

  1. Phone/__init__.py

To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows:

from Pots import Pots
from Isdn import Isdn
from G3 import G3

After you've added these lines to __init__.py, you have all of these classes available when you've imported the Phone package.

# Now import your Phone Package.
import Phone

Phone.Pots()
Phone.Isdn()
Phone.G3()
When the above code is executed, it produces the following result:
I'm Pots Phone
I'm 3G Phone
I'm ISDN Phone
NOTE
In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. You can also define different Python classes in those files and then you can create your packages out of those classes.

 

Take Test

What are Python Dictionaries? 

Dictionaries are one of Python's powerful datatypes, behaves like the popular Json object, in fact, understanding Python's dictionary gives you almost every thing you need to know about Json objects because Json can be viewed as Python's dictionaries nested. You could also see it JavaScript's object. They may however not be completely the same.

 

A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. 

When I say mutable, I mean it can be changed after it is created.
Hope you remember that is not possible with all python data types,
especially tuples.

Dictionaries consist of pairs (called items) of keys and their corresponding values.

 Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows:

 dict = {'Effie': '2341', 'Beth': '9102', 'Cecil': '3258'} 

You can create dictionary in the following way as well: 

dict1 = { 'abc': 456 }; dict2 = { 'abc': 123, 98.6: 37 } 

Each key is separated from its value by a colon (:), the items  in the dictionary are separated by commas, and the whole set is enclosed in curly braces.  An empty dictionary without any items is written with just two curly braces, like this: 

dictionary={}. 

Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. 

Accessing Values in Dictionary 

To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. 

Following is a simple example: 

 

dict = {'Name': 'Effie', 'Age': 7, 'Class': 'First'} 
print ("dict['Name']: ", dict['Name']) 
print ("dict['Age']: ", dict['Age']) 

When the above code is executed, it produces the following result: 

dict['Name']: Effie dict['Age']: 7 

If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows: 

dict = {'Name': 'Effie', 'Age': 7, 'Class': 'First'} 
print ("dict['Effie']: ", dict['Alice']) 
When the above code is executed, it produces the following result: dict['Effie']: 
Traceback (most recent call last): File "test.py", line 4, in print ("dict['Alice']: ", dict['Alice']) KeyError: 'Alice' 

Updating Dictionary 

You can update a dictionary by adding a new entry or item (i.e., a key-value pair), modifying an existing entry, or deleting an existing entry as shown below in the simple example: 

dict = {'Name': 'Effie', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # update existing entry 
dict['School'] = "Victory School" # Add new entry 
print ("dict['Age']: ", dict['Age']) 
print ("dict['School']: ", dict['School']) 

When the above code is executed, it produces the following result: 

dict['Age']: 8 
dict['School']: Victory School 

Delete Dictionary Elements 

You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation. 

To explicitly remove an entire dictionary, just use the del statement. 

Following is a simple example: 

dict = {'Name': 'Effie', 'Age': 7, 'Class': 'First'}
del dict['Name'] # remove entry with key 'Name' 
dict.clear() # remove all entries in dict 
del dict # delete entire dictionary 
print ("dict['Age']: ", dict['Age']) 
print ("dict['School']: ", dict['School']) 

This will produce the following result. Note an exception raised, this is because after del dict, dictionary does not exist any more: dict['Age']: 

Traceback (most recent call last): File "test.py", line 8, in print ("dict['Age']: ", dict['Age']) 
TypeError: 'type' object is unsubscriptable 

Note: del() method is discussed in subsequent section. 

Properties of Dictionary Keys Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys. There are two important points to remember about dictionary keys 

  • More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins. Following is a simple example: 
  • dict = {'Name': 'Effie', 'Age': 7, 'Name': 'Ellie'} 
    print ("dict['Name']: ", dict['Name']) 
  • When the above code is executed, it produces the following result: 
  • dict['Name']: Ellie 
  • Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like ['key'] (a list) is not allowed. Following is a simple example:
  • dict = {['Name']: 'Effie', 'Age': 7} 
  • print ("dict['Name']: ", dict['Name']) 
  • When the above code is executed, it produces the following result: 
  • Traceback (most recent call last): File "test.py", line 3, in dict = {['Name']: 'Effie', 'Age': 7} 
  • TypeError: list objects are unhashable Built-in 

Dictionary Functions & Methods 

Python includes the following dictionary functions: 

 

Function
DESCRIPTION
cmp(dict1, dict2)
Compares elements of both dict.
len(dict)
Gives the total length of the dictionary
str(dict)
Produces a printable string representation of a dictionary
type(variable) 
Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type

 

Python includes following dictionary methods

Methods
description
dict.clear()
Removes all elements of dictionary dict
dict.copy() 
Returns a shallow copy of dictionary dict
dict.fromkeys()
Create a new dictionary with keys from seq and values set to value.
dict.get(key, default=None)
For key 'key', returns value or default if key not in dictionary
dict.has_key(key)
Returns true if key in dictionary dict, false otherwise
dict.items()
Returns a list of dict's (key, value) tuple pairs
dict.keys() 
Returns list of dictionary dict's keys
dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict
dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
dict.values()
Returns list of dictionary dict's values

 

Take Test

 

What is an Error?

I am sure you have already encountered an error trying to run your first program and you might have been a bit frustrated because what was printed out was confusing and almost unreadable. Well, welcome to the error side of programming. Learning to handle errors will help you to write applications faster and make your application more secured.

Here's one thing you should know: You do not stop committing errors because you have become more experienced at programming, you only reduce the errors and are able to foresee an impeding error and therefore are able to handle it.

You are also able to read and understand error messages which helps in correcting your program just after reading the error message. Basically, you have just been told why you should treat this topic as an important one.

What is an Exception?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. This means that exceptions are either logical or syntax related mistakes that you have made in your application and therefore prevents the application from running successfully.

Whenever the interpreter encounters a problem, it stops executing the code even if the rest of the code after the error is correct.

Imagine you send a robot to deliver a message for you on foot in a nearby town. Whenever this robot kicks a stone on the way, it returns and tells you that the rest of the journey is too dangerous so it was not able to deliver your message. And in asking for the reason for this conclusion, the robot tells you it was because he kicked a stone.

You should probably tell the robot to look on the ground carefully while walking and whenever it kicks a stone, it should continue the journey assuring him that the rest of the journey is safe.

Python does something like this every time it runs your program. The interpreter does not tolerate any mistake at all and cannot make intelligent guesses in order to deal with problems unless you provide a way out for it; this is called Error Handling. In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error. When a Python script raises an exception (kicks a stone), it must either handle the exception immediately (know what to do) otherwise it would terminate and come out (come home without delivering the message).

Handling an exception

If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.

Syntax

Here is simple syntax of try....except...else blocks:

try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block.

Here are few important points about the above-mentioned syntax

A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions. You can also provide a generic except clause, which handles any exception. After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception. The else-block is a good place for code that does not need the try: block's protection. Example: Here is simple example, which opens a file and writes the content in the file and comes out gracefully because there is no problem at all:

try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
   fh.close()
    

 

This will produce the following result:

Written content in the file successfully

Example: Here is one more simple example, which tries to open a file where you do not have permission to write in the file, so it raises an exception:

try:
   fh = open("testfile", "r")
   fh.write("This is my test file for exception handling!!")
except IOError:
   print "Error: can\'t find file or read data"
else:
   print "Written content in the file successfully"
This will produce the following result:

Error: can't find file or read data
The except clause with no exceptions:
You can also use the except statement with no exceptions defined as follows:

try:
   You do your operations here;
   ......................
except:
   If there is any exception, then execute this block.
   ......................
else:
   If there is no exception then execute this block.

This kind of a try-except statement catches all the exceptions that occur.

Tip
Using this kind of try-except statement is not considered a good programming practice though, because it catches all exceptions but does not make the programmer identify the root cause of the problem that may occur. So it may be better for catching all other exceptions after catching speccific ones.

The except clause with multiple exceptions

You can also use the same except statement to handle multiple exceptions as follows:

try:
   You do your operations here;
   ......................
except(Exception1[, Exception2[,...ExceptionN]]]):
   If there is any exception from the given exception list,
   then execute this block.
   ......................
else:
   If there is no exception then execute this block.

 

The try-finally clause

You can use a finally: block along with a try: block. The finally block is a place to put any code that must execute, whether the try-block raised an exception or not. The syntax of the try-finally statement is this:

try:
   You do your operations here;
   ......................
   Due to any exception, this may be skipped.
finally:
   This would always be executed.
   ......................
Tip
You can provide except clause(s), or a finally clause, but not both. You can not use else clause as well along with a finally clause.

Example:

try:
   fh = open("testfile", "w")
   fh.write("This is my test file for exception handling!!")
finally:
   print "Error: can\'t find file or read data"

If you do not have permission to open the file in writing mode, then this will produce the following result:

Error: can't find file or read data Same example can be written more cleanly as follows:

try:
   fh = open("testfile", "w")
   try:
      fh.write("This is my test file for exception handling!!")
   finally:
      print "Going to close the file"
      fh.close()
except IOError:
   print "Error: can't find file or read data"
    

When an exception is thrown in the try block, the execution immediately passes to the finally block. After all the statements in the finally block are executed, the exception is raised again and is handled in the except statements if present in the next higher layer of the try-except statement.

Argument of an Exception

An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary by exception. You capture an exception's argument by supplying a variable in the except clause as follows:

try:
   You do your operations here;
   ......................
except ExceptionType, Argument:
   You can print value of Argument here...
    

 

If you are writing the code to handle a single exception, you can have a variable follow the name of the exception in the except statement. If you are trapping multiple exceptions, you can have a variable follow the tuple of the exception. This variable will receive the value of the exception mostly containing the cause of the exception. The variable can receive a single value or multiple values in the form of a tuple. This tuple usually contains the error string, the error number, and an error location. Example: Following is an example for a single exception:

# Define a function here.
def temp_convert(var):
   try:
      return int(var)
   except ValueError, Argument:
      print "The argument does not contain numbers\n", Argument

# Call above function here.
temp_convert("xyz")
    

This would produce the following result:

The argument does not contain numbers
invalid literal for int() with base 10: 'xyz'

Raising an exceptions

You can raise exceptions in several ways by using the raise statement. The general syntax for the raise statement.

Syntax

raise [Exception [, args [, traceback]]] Here, Exception is the type of exception (for example, NameError) and argument is a value for the exception argument. The argument([Exception [, args [, traceback]]]) is optional; if not supplied, the exception argument is None. The final argument, traceback, is also optional (and rarely used in practice), and if present, is the traceback object used for the exception. Example: An exception can be a string, a class or an object. Most of the exceptions that the Python core raises are classes, with an argument that is an instance of the class. Defining new exceptions is quite easy and can be done as follows:

def functionName( level ):
   if level < 1:
      raise "Invalid level!", level
      # The code below to this would not be executed
      # if we raise the exception
    
Tip
In order to catch an exception, an "except" clause must refer to the same exception thrown either class object or simple string. For example, to capture above exception, we must write our except clause as follows:
try:
   Business Logic here...
except "Invalid level!":
   Exception handling here...
else:
   Rest of the code here...

User-Defined Exceptions

Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions. Here is an example related to RuntimeError. Here, a class is created that is subclassed from RuntimeError. This is useful when you need to display more specific information when an exception is caught. In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to create an instance of the class Networkerror.

class Networkerror(RuntimeError):
   def __init__(self, arg):
      self.args = arg

 

So once you defined above class, you can raise your exception as follows:

try:
   raise Networkerror("Bad hostname")
except Networkerror,e:
   print e.args

 

Python has been an object-oriented language from day one. Because of this, creating and using classes and objects are downright easy. This chapter helps you become an expert in using Python's object-oriented programming support.

What is an object?

An object is an instance of a class. Now, that sentence is really short and very confusing. You will note that understanding what a class, and an instance are is essential to understanding the definition given. Getting this topic is very essential because creating your own classes is very important to maintaining your code, making your application easy to read and understand.


This is a very important topic in programming.

Understanding this concept in programming will help you organize and be able to reuse your own code over and over again. Reusing your code will in turn save you a lot of time while coding. One more thing, it will help you write concise and shorter codes.  This concept is in fact, the one upon which every python code you know is written because every 'thing' from strings to any other type you know in python is an object. So you will learn how to make or write your own data types using this classes, is that not amazing?

Creating Classes. 

You follow a particular syntax to create classes in every language, but the general idea is to create a blueprint or an outline of a particular module or class, define (def) it's various parts (functions or methods) and what in particular each of these functions or methods do. 

Declaring Objects.

After creating your class, you will require an object, which can "wear" or "take up" the form, attributes (characteristics) and methods(functions) of the class, this is the way to make your blueprint useful. You might find the idea a bit daunting at first, you may be asking, "how do I put all of these together?" Let's look at classes this way; you are human (an object of the human class or  an object of the human definition) and you have parts (functions or methods) and these parts perform specific actions, and some, even multiple actions. Let's take the hand for an example and let's assume for the purpose of getting this idea clearly, that you only use the hand to throw a ball. This implies that anytime you want to use the hand, you will need a ball. Because..? Of course, because as we agreed earlier, the hand is for throwing a ball.


Arguments/Parameters.

We agreed that we needed a ball to use the hand. Do not get so lost in the analogy, the hand is a part of the human class, so the hand represents a function. Now, the name given to the ball is an argument or a parameter. Our function needs a ball (a parameter) in order to throw. So, any time you call this function, you need to supply it with a ball. You will do this in Python like this:

"""Create an object of the human class and name it.
We are assuming the Human class has already been defined.
We will learn how to define a class ourselves later"""
Kofi =  Human()
# Now call the function and supply it with a ball
Kofi.hand('ball')

 

Default arguments.

Sometimes, during the creation of your class, you could provide a default argument to your function(s), so that whenever a user of your class calls that function without providing it with an argument, the function will go and pick up that default argument you provided to work with. Make this clear in your documentation so that the default behavior of your function will not be a surprise. Try this.

# importing the sys class
import sys
print("Hello")
sys.exit(0)

 

Now try this:

import sys
print("hello")
sys.exit()

 

So the first time, you supplied an argument and it worked fine, the next, you did not, it still did work fine. That is because, the one who created this function called "exit" from the class sys, supplied it with a default argument which gets supplied to the function in case you did not provide any argument like in the second case.

Always argue?

However, you will agree with me that not every function (part) should require an argument to work. And yes, it is not every function that we create that should require an argument. So you can create a function like that, which when called, it just does something without requiring the user to supply anything. Can you think of any situation like that? Let's try this: If I said to you: "polish", you should definitely ask me for an argument, that is "what should I polish?", but if I said "jump", you sure can do that without any questions. Let's put this in code. Note that what you see between the parenthesis is/are what we call arguments or parameters.

# Let's define our function called polish
# Which always needs an argument
def polish(item):
	print("polished ",item)

So, calling this function without providing an argument (what to polish) should raise an error. Now, let's create one that does not require anything.

# Creating a function that does not require argument.
def jump():
	print("Jumped")

 The above requires no argument at all, so providing one will raise an error.

Take Note
A typical function should return a value, we use the keyword return to terminate a function. So, if it is a calculation, then the return will return the final answer of the calculation. print was used just to make things clearer. Read on Functions from our lessons to get more on different types of arguments and possibilities with functions.

Like this:

def area_of_triangle(base,height):
	answer = 0.5*base*height
	return answer

 

This is because, we typically do not print the value returned from a function but we use it for something else, we will like to leave that to whoever uses the function, whether the person would like to just print the "answer" to the console, pass it to another function, display it on a web-page or even pass it on to an artificial intelligence system, anything.

Putting it all together

Now, we can put all of these methods in one class, so when we create an object of that class, the object can perform all of these functions. So, we could define thousands of functions in one class and then create objects which will inherit these functions. You might already be guessing some powerful things you could use this ability for. It even gets much more exciting to know that there is the ability to inherit from other classes. So, a class can serve as a parent class to other classes who then become the child classes. the child classes "inherit" the abilities of their parent class and add their own abilities to those, explaining their power. We will talk about inheritance later, for now, lets create our own class, a Human class.

# Use the keyword class to begin and give it a name
class Human:

""" We then initialize the class using __init__ Function.
This gets called whenever a Human object is created"""
 
        # The __init__ Function with default argument
        # for the Name being Kojo
	def __init__(self,Name="Kojo"):
		self.name = Name
        
        # Function that returns the Name
	def get_name(self):
		return self.name

        # Function that is used to set a new name
	def set_name(self,Name):
		self.name = Name
		return self.name
         
        # Function that polishes a given item
	def polish(self,item):
		value_added = "polished"
		return item,value_added
       
        # Function that makes the object jump
	def jump(self):
		return self.name, "jumped"

 

""" Let's create an object of our class.
Remember our __init__ requires two arguments:
Which are self (the object we created, so it's already supplied
And a Name """

# Baby is the object and Kofi is the name
Baby = Human("Kofi")

 

# Let's get the name of our Human object that
# we referenced as Baby.

print(Baby.get_name())
>>>Kofi

 

From our __init__ method, we have provided a default Name for any object created, so if you created an object of our Human class without passing any Name, the get_name method will still return that default argument, whcih in this case is Kojo. But if you do provide a Name like in the above, then that Name will override the default Name(Kojo).

# Let's set a new name for our object Baby
print(Baby.set_name("Adwoa")
>>>Adwoa

 

The self argument gets supplied to our method anytime we call the object so there's no need to supply it ourselves. The get method is used to get attributes (characteristics, like Name in this case) of our object, in this case we wrote one that gets the Name of the object, there are in_built get methods, also the set methods are used to modify or change those attributes, we also wrote an example ourselves, which sets a new Name for our object. We then wrote two other methods, one is polish which takes an argument, particularly, the item to polish and returns a "polished" item. The other method is jump, which just makes the object jumps. So there, we have created our own class, provided our own getters and setters, and other methods. We also learnt how to create an object of our class and how to put that object in use.

Can you think of other methods you could add?

How about some other attributes apart from the Name?, like skin color, hair color...

FootNote: Some PEP8 rules were broken on purpose, this is to make codes clearer in our opinion and to make differentiating easier. In the case of the variable Naming, where we used "Name" instead of "name" is an example.

 

Further Readings

A class is an outline or a blueprint for creating an object. It is how an object will look like and how the various parts of that object will function. Imagine an outline for a car, not the car itself but the outline, the various parts that will move to move the whole car. All of these parts must be pre-defined and theoretically tested before creating the real object which is the physical car. So, the physical car is a manifestation of the "plan" (outline).

Classes help hide data that should not be accessible to the user from the user (data abstraction).

Using the car example, you realise that while using a car, let's focus on driving and loading your stuff inside the car as it's only usage (well, is there another usage?), you are allowed by the company that made the car directly be able to control some parts of the car and even have a handbook on how to effectively control these parts.

However, some parts of the car are a bit restricted because you will mess things up if you were an allowed easy access.

This is exactly the concept that classes help you achieve and also allow you to group functions of the same entity together. If you are wondering what these functions could be just think of the car example again: the functions will include accelerate, so whenever the user calls accelerate, the car moves forward, reverse, break, steer right, steer left among others.

Basically, this is the idea, when you create a class of a car, then you have created a car with all its functions but the car remains invisible and unusable until an object which is a physical manifestation of that car class is created. The object assumes all the definitions of that class, and performs all the functions defined in the class suite.

When I say human, you definitely have an idea what a human looks like and the things that a human can do - that is the class. Now, you will agree that every human have their unique features and abilities even though they all belong to the same blueprint (The Human Class).

In progrmming, this concept of every unique human being (object of the human class) having their own unique features is implemented by creating only one class but having as many objects of that class as possible and each object having their own unique features (class variables).

Inheriting features from an "older" class is allowed, just like you inherited some features from your Mum or Dad.

Did you get the point?

In beginning, I said an object is an instance of a class. do you get the point now? An object is an entity of a class, an object is the manifestation of a class. When I say this is a dog, you definitely have a blueprint or outline of a dog in your mind to agree or disagree with, so goes for any object, the object must have the features of it's class.

Tip
A dog must look like what dogs look like, and human like humans look like. In that sentence, "a dog" is an object or an instance of the "dogs" class and "human" is an object of the "humans" class.

 

Now let's move on to some terminologies and their definitions before creating our own classes and objects.

Overview of OOP Terminology

  • Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
  • Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables aren't used as frequently as instance variables are.
  • Data member: A class variable or instance variable that holds data associated with a class and its objects.
  • Function overloading: The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects (arguments) involved.
  • Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class.
  • Inheritance : The transfer of the characteristics of a class to other classes that are derived from it.
  • Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
  • Instantiation: The creation of an instance of a class.
  • Method: A special kind of function that is defined in a class definition.
  • Object: A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.
  • Operator overloading: The assignment of more than one function to a particular operator.

Creating Classes

The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows:

class ClassName:
   'Optional class documentation string'
   class_suite

The class has a documentation string, which can be accessed via ClassName.__doc__. The class_suite consists of all the component statements defining class members, data attributes and functions. Example: Following is the example of a simple Python class:

class Employee:
    #This is the docstring
   'Common base class for all employees'
    # class variable
   empCount = 0
    # initialization method
   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
    #Other class methods
   def displayCount(self):
     print ("Total Employee %d" % Employee.empCount)

   def displayEmployee(self):
      print ("Name : ", self.name,  ", Salary: ", self.salary)

The variable empCount is a class variable whose value would be shared among all instances of a this class. This can be accessed as Employee.empCount from inside the class or outside the class. The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. You declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you don't need to include it when you call the methods.

Creating instance objects

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.

#This would create first object of Employee class called Effie
Effie = Employee("Effie", 6000)
#This would create second object of Employee class called Ellie
Ellie = Employee("Ellie", 4000)

 

So, those two objects (emp1 and emp2) belong to the same class called Employee and have their unique features which differentiates one from the other. We created only one class but we could create two objects from that same class without wirting a class for the second object all over again. If there were a thousand Employees, we could just create a thousand objects from the same class with thier own Names and Salaries.

Accessing attributes

You access the object's attributes using the dot operator with object. Class variable would be accessed using class name as follows:

Effie.displayEmployee()
Ellie.displayEmployee()
print ("Total Employee %d" % Employee.empCount)

Now, putting all the concepts together:

class Employee:
   'Common base class for all employees'
   empCount = 0

   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1

   def displayCount(self):
     print ("Total Employee %d" % Employee.empCount)

   def displayEmployee(self):
      print ("Name : ", self.name,  ", Salary: ", self.salary)

#This would create first object of Employee class called emp1
emp1 = Employee("Effie", 2000)
#This would create second object of Employee class called emp2
emp2 = Employee("Theo", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee %d" % Employee.empCount)
When the above code is executed, it produces the following result:

Name :  Effie ,Salary:  3000
Name :  Theo ,Salary:  5000
Total Employee 2
    

You can add, remove or modify attributes of classes and objects at any time:

emp1.age = 7  # Add an 'age' attribute.
emp1.age = 8  # Modify 'age' attribute.
del emp1.age  # Delete 'age' attribute.

Instead of using the normal statements to access attributes,you can use following functions:

The getattr(obj, name[, default]) : to access the attribute of object.

The hasattr(obj,name) : to check if an attribute exists or not.

The setattr(obj,name,value) : to set an attribute.
    If attribute does not exist, then it would be created.

The delattr(obj, name) : to delete an attribute.

hasattr(emp1, 'age')    # Returns true if 'age' attribute exists
getattr(emp1, 'age')    # Returns value of 'age' attribute
setattr(emp1, 'age', 8) # Set attribute 'age' at 8
delattr(empl, 'age')    # Delete attribute 'age'
    

Built-In Class Attributes

Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute: __dict__ : Dictionary containing the class's namespace. __doc__ : Class documentation string or None if undefined. __name__: Class name. __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. For the above class let's try to access all these attributes:

class Employee:
   'Common base class for all employees'
   empCount = 0

   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1

   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary

 

print ("Employee.__doc__:", Employee.__doc__)
print ("Employee.__name__:", Employee.__name__)
print ("Employee.__module__:", Employee.__module__)
print ("Employee.__bases__:", Employee.__bases__)
print ("Employee.__dict__:", Employee.__dict__)
    

When the above code is executed, it produces the following result:

Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount':
, 'empCount': 2,
'displayEmployee': ,
'__doc__': 'Common base class for all employees',
'__init__': }
            

Destroying Objects (Garbage Collection)

Python deletes unneeded objects (built-in types or class instances) automatically to free memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed garbage collection. Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes. An object's reference count increases when it's assigned a new name or placed in a container (list, tuple or dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope. When an object's reference count reaches zero, Python collects it automatically.

a = 40      # Create object <40>
b = a       # Increase ref. count  of <40>
c = [b]     # Increase ref. count  of <40>

del a       # Decrease ref. count  of <40>
b = 100     # Decrease ref. count  of <40>
c[0] = -1   # Decrease ref. count  of <40>
    

You normally won't notice when the garbage collector destroys an orphaned instance and reclaims its space. But a class can implement the special method __del__(), called a destructor, that is invoked when the instance is about to be destroyed. This method might be used to clean up any nonmemory resources used by an instance. Example: This __del__() destructor prints the class name of an instance that is about to be destroyed:

class Point:
   def __init( self, x=0, y=0):
      self.x = x
      self.y = y
   def __del__(self):
      class_name = self.__class__.__name__
      print class_name, "destroyed"

 

pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3
    

When the above code is executed, it produces following result:

3083401324 3083401324 3083401324
Point destroyed
    
Tip
Ideally, you should define your classes in separate file, then you should import them in your main program file using import statement. Kindly check PywE Modules chapter for more details on importing modules and classes.

Class Inheritance

Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name. The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent. Just the way you would inherit the physical features of your parents and even their properties.

Syntax

Derived classes are declared much like their parent class; however, a list of base classes to inherit from are given after the class name:

class SubClassName (ParentClass1[, ParentClass2, ...]):
   'Optional class documentation string'
   class_suite

Example:

class Parent:        # define parent class
   parentAttr = 100
   def __init__(self):
      print ("Calling parent constructor")

   def parentMethod(self):
      print ('Calling parent method')

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print "Parent attribute :", Parent.parentAttr

class Child(Parent): # define child class
   def __init__(self):
      print ("Calling child constructor")

   def childMethod(self):
      print ('Calling child method')
       

 

c = Child()          # instance of child
c.childMethod()      # child calls its method
c.parentMethod()     # calls parent's method
c.setAttr(200)       # again call parent's method
c.getAttr()          # again call parent's method
    

When the above code is executed, it produces the following result:

Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200
    

Similar way, you can derive a class from multiple parent classes as follows:

class A:        # define your class A
.....

class B:         # define your calss B
.....

class C(A, B):   # subclass of A and B
.....
    

You can use issubclass() or isinstance() functions to check a relationships of two classes and instances. The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the superclass sup. The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance of a subclass of Class

Overriding Methods

You can always override your parent class methods. One reason for overriding parent's methods is because you may want special or different functionality in your subclass. Example:

class Parent:        # define parent class
   def myMethod(self):
      print ('Calling parent method')

class Child(Parent): # define child class
   def myMethod(self):
      print ('Calling child method')

c = Child()          # instance of child
c.myMethod()         # child calls overridden method
    

When the above code is executed, it produces the following result:

Calling child method
    

Base Overloading Methods

Following table lists some generic functionality that you can override in your own classes:

 

 

Method
Description & Sample Call
__init__ ( self [,args...] )

Constructor (with any optional arguments)

Sample Call : obj = className(args)

__del__( self )

Destructor, deletes an object

Sample Call : del obj

__repr__( self )

Evaluatable string representation

Sample Call : repr(obj)

__str__( self )

Printable string representation

Sample Call : str(obj)

__cmp__ ( self, x )

Object comparison

Sample Call : cmp(obj, x)

Overloading Operators

Suppose you've created a Vector class to represent two-dimensional vectors, what happens when you use the plus operator to add them? Most likely Python will yell at you. You could, however, define the __add__ method in your class to perform vector addition and then the plus operator would behave as per expectation: Example:

class Vector:
   def __init__(self, a, b):
      self.a = a
      self.b = b

   def __str__(self):
      return ('Vector (%d, %d)' % (self.a, self.b))

   def __add__(self,other):
      return (Vector(self.a + other.a, self.b + other.b))

 

v1 = Vector(2,10)
v2 = Vector(5,-2)
print (v1 + v2)

When the above code is executed, it produces the following result:

Vector(7,8)

Data Hiding

Remember we talked about hiding certain data from the user in order to prevent the user from messing up things? An object's attributes may or may not be visible outside the class definition. For these cases, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders. Example:

class JustCounter:
   __secretCount = 0

   def count(self):
      self.__secretCount += 1
      print (self.__secretCount)

counter = JustCounter()
counter.count()
counter.count()
print (counter.__secretCount)
    

 

When the above code is executed, it produces the following result:

1
2
Traceback (most recent call last):
  File "test.py", line 12, in 
    print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'

 

Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName. If you would replace your last line as following, then it would work for you: .........................

print (counter._JustCounter__secretCount)

When the above code is executed, it produces the following result:

1
2
2