Python is a high-level, Interpreted, Interactive and object-oriented scripting language. Python was designed to be highly readable and uses English Keywords frequently. It has fewer syntatical constructions than other languages (This means that you need few lines of code to achieve just what other languages will use many lines of codes for).
You might have found some words in the above Introduction difficult especially if you do not have any programming experience or are a self-taught programmer like myself. So let's interpret some of them:
I get this question most often than not from people who visit this site or get to know that I handle Python tutorials. Basically, I will tell you about what I have used Python for and then I may add what other's have also used Python for.
This is not to tell you all that Python can do because it depends on your level of knowledge and creativity also.
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages. Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL). Python is now maintained by a core development team at the institute, although Guido van Rossum still holds a vital role in directing its progress.
Python's feature highlights include:
Apart from the above-mentioned features, Python has a big list of good features, few are listed below:
To begin with, let's write our first python script. You will find out how easy python is in a moment. Let's head over to a video from Socratica for a Hello World script.
Disclaimer: This video is not a property of PywE and is used for learning purposes only. It belongs to Socratica. (c) Copyright Socratica.
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
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']
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. print_this_to_screen
, all are valid examples.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:
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:
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.
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. |
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.
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
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.
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
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.)
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
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.
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 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.
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
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 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
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. |
The module also defines two mathematical constants:
Constants | Description |
---|---|
pi | The mathematical constant pi. |
piThe mathematical constant pi. e | The mathematical constant e. |
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.
Let's have a look on all operators one by one.
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 |
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
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 |
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 |
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. |
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 |
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). |
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 |
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.
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.
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.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")
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
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.
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.
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.
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
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]
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 |
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
The rules applied to sample codes are as follows (its only one):
The syntax used is that of Python 2 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.
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma-separated values between parentheses also. For example:
tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d"
The empty tuple is written as two parentheses containing nothing:
tup1 = ()
To write a tuple containing a single value you have to include a comma, even though there is only one value:
tup1 = (50,)
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on.
print "value of x,y:",print 1,2
value of x,y : 1 2
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example:
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
When the above code is executed, it produces the following result:
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Tuples are immutable which means you cannot update or change the values of tuple elements.
You are able to take portions of existing tuples to create new tuples as the following example demonstrates:
#!/usr/bin/python tup1 = (12, 34.56); tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2; print tup3;
When the above code is executed, it produces the following result:
(12, 34.56, 'abc', 'xyz')
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement. Following is a simple example:
#!/usr/bin/python
tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup : " print tup
This will produce following result. Note an exception raised, this is because after del tup tuple does not exist any more:
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in print tup;
NameError: name 'tup' is not defined
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.
In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter :
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 |
Because tuples are sequences, indexing and slicing work the same way for tuples as they do for strings. Assuming following 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 |
Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples, as indicated in these short examples:
#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz'; x, y = 1, 2; print "Value of x , y : ", x,y;
When the above code is executed, it produces the following result:
abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2
Python includes the following tuple functions:
.cmp(tuple1, tuple2) Compares elements of both tuples.
.len(tuple) Gives the total length of the tuple.
.max(tuple) Returns item from the tuple with max value.
.min(tuple) Returns item from the tuple with min value.
.tuple(seq) Converts a list into tuple.
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. |
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.
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.
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.
Write a function that takes length as input and returns the perimeter of a square.
You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
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
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]
You can call a function by using the following types of formal 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 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
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
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
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.
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 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
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:
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
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".
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 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")
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).
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
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.
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.
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.
When you import a module, the Python interpreter searches for the module in the following sequences:
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 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
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 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.
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)
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:
Now, create one more file __init__.py
in Phonedirectory:
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
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
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.
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.
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.
When you import a module, the Python interpreter searches for the module in the following sequences:
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 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
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 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.
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)
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:
Now, create one more file __init__.py
in Phonedirectory:
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
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.
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'Â
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Â
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
dict = {'Name': 'Effie', 'Age': 7, 'Name': 'Ellie'}Â
print ("dict['Name']: ", dict['Name'])Â
dict['Name']: EllieÂ
dict = {['Name']: 'Effie', 'Age': 7}
print ("dict['Name']: ", dict['Name'])
Traceback (most recent call last): File "test.py", line 3, in dict = {['Name']: 'Effie', 'Age': 7}
TypeError: list objects are unhashable Built-in
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 |
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 |
Storage System Operation
print ("Python is really a great language,", "isn't it?")
This would produce the following result on your standard screen:
Python is really a great language, isn't it?
Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are:
The raw_input([prompt_string]) function reads one line from standard input and returns it as a string (removing the trailing newline).
str = raw_input("Enter your input: ") print ("Received input is : ", str)
This would prompt you to enter any string and it would display same string on the screen. When I typed "Hello Python!", its output is like this:
Enter your input: Hello Python Received input is : Hello Python
The input Function: The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you.
input
as raw_input
is used in version 2. Input
in Python 3 is used to accept strings not integers, the string received can then be converted to the appropriate data type using the appropriate method discussed earlier.str = input("Enter your input: "); print ("Received input is : ", str)
This would produce the following result against the entered input:
Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 20, 30, 40]
Until now, you have been reading and writing to the standard input and output. Now, we will see how to play with actual data files. Python provides basic functions and methods necessary to manipulate files by default. You can do your most of the file manipulation using a file object.
Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.
file_object = open(file_name [, access_mode][, buffering])
Here is paramters' detail
Here is a list of the different modes of opening a file:
Modes | Description |
---|---|
r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
r+ | Opens a file for both reading and writing. The file pointer will be at the beginning of the file. |
rb+ | Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. |
w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
wb | Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
w+ | Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
wb+ | Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
a | Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
ab | Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
a+ | Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
ab+ | Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
Once a file is opened and you have one fileobject, you can get various information related to that file. Here is a list of all attributes related to file object
# Open a file fo = open("foo.txt", "wb") print ("Name of the file: ", fo.name) print ("Closed or not : ", fo.closed) print ("Opening mode : ", fo.mode) print ("Softspace flag : ", fo.softspace)
This would produce the following result:
Name of the file: foo.txt Closed or not : False Opening mode : wb Softspace flag : 0
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.
fileObject.close()
# Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Close opend file fo.close()
This would produce the following result:
Name of the file: foo.txt
The file object provides a set of access methods to make our lives easier. We would see how to use read() and write() methods to read and write files.
The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text. The write() method does not add a newline character ('\n') to the end of the string:
fileObject.write(string)
Here, passed parameter is the content to be written into the opened file.
# Open a file fo = open("foo.txt", "wb") fo.write( "Python is a great language.\nYeah its great!!\n"); # Close opend file fo.close()
The above method would create foo.txt file and would write given content in that file and finally it would close that file. If you would open this file, it would have following content.
Python is a great language. Yeah its great!!
The read() method reads a string from an open file. It is important to note that Python strings can have binary data and not just text.
fileObject.read([count])
Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of file.
Let's take a file foo.txt, which we have created above.
# Open a file fo = open("foo.txt", "r+") str = fo.read(10); print "Read String is : ", str # Close opend file fo.close()
This would produce the following result:
Read String is : Python is
The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file. The seek(offset[, from]) method changes the current file position. The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved. If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position as the reference position and if it is set to 2 then the end of the file would be taken as the reference position.
Let's take a file foo.txt, which we have created above.
# Open a file fo = open("foo.txt", "r+") str = fo.read(10); print "Read String is : ", str # Check current position position = fo.tell(); print "Current file position : ", position # Reposition pointer at the beginning once again position = fo.seek(0, 0); str = fo.read(10); print "Again read String is : ", str # Close opend file fo.close()
This would produce the following result:
Read String is : Python is Current file position : 10 Again read String is : Python is
Python os module provides methods that help you perform file-processing operations, such as renaming and deleting files. To use this module you need to import it first and then you can call any related functions.
The rename() method takes two arguments, the current filename and the new filename.
os.rename(current_file_name, new_file_name)
Following is the example to rename an existing file test1.txt:
import os # Rename a file from test1.txt to test2.txt os.rename( "test1.txt", "test2.txt" )
You can use the remove() method to delete files by supplying the name of the file to be deleted as the argument.
os.remove(file_name)
Following is the example to delete an existing file test2.txt:
import os # Delete file test2.txt os.remove("text2.txt")
All files are contained within various directories, and Python has no problem handling these too. The os module has several methods that help you create, remove and change directories.
You can use the mkdir() method of the osmodule to create directories in the current directory. You need to supply an argument to this method which contains the name of the directory to be created.
os.mkdir("newdir")
Following is the example to create a directory test in the current directory:
import os # Create a directory "test" os.mkdir("test")
You can use the chdir() method to change the current directory. The chdir() method takes an argument, which is the name of the directory that you want to make the current directory.
os.chdir("newdir")
Following is the example to go into "/home/newdir" directory:
import os # Changing a directory to "/home/newdir" os.chdir("/home/newdir") The getcwd() Method: The getcwd() method displays the current working directory. Syntax: os.getcwd()
Following is the example to give current directory:
import os # This would give location of the current directory os.getcwd() The rmdir() Method: The rmdir() method deletes the directory, which is passed as an argument in the method.
Before removing a directory, all the contents in it should be removed.
os.rmdir('dirname')
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified name of the directory, otherwise it would search for that directory in the current directory.
import os # This would remove "/tmp/test" directory. os.rmdir( "/tmp/test" )
There are three important sources, which provide a wide range of utility methods to handle and manipulate files & directories on Windows and Unix operating systems. They are as follows:
The Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard. You can choose the right database for your application. Python Database API supports a wide range of database servers:
Here is the list of available Python database interfaces: Python Database Interfaces and APIs.You must download a separate DB API module for each database you need to access. For example, if you need to access an Oracle database as well as a MySQL database, you must download both the Oracle and the MySQL database modules. The DB API provides a minimal standard for working with databases using Python structures and syntax wherever possible. This API includes the following:
We would learn all the concepts using MySQL, so let's talk about MySQLdb module only.
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API.
Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it:
import MySQLdb If it produces the following result, then it means MySQLdb module is not installed: Traceback (most recent call last): File "test.py", line 3, in import MySQLdb ImportError: No module named MySQLdb To install MySQLdb module, download it from MySQLdb Download page and proceed as follows: $ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2.tar $ cd MySQL-python-1.2.2 $ python setup.py build $ python setup.py install
Before connecting to a MySQL database, make sure of the followings:
Â
Example: Following is the example of connecting with MySQL database "TESTDB"
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. cursor.execute("SELECT VERSION()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print "Database version : %s " % data # disconnect from server db.close()
While running this script, it is producing the following result in my Linux machine. Database version : 5.0.45 If a connection is established with the datasource, then a Connection Object is returned and saved into db for further use, otherwise db is set to None. Next, db object is used to create a cursor object, which in turn is used to execute SQL queries. Finally, before coming out, it ensures that database connection is closed and resources are released.
Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor. Example: First, let's create Database table EMPLOYEE:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # Create table as per requirement sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" cursor.execute(sql) # disconnect from server db.close()
INSERT operation is required when you want to create your records into a database table. Example: Following is the example, which executes SQL INSERT statement to create a record into EMPLOYEE table:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
Above example can be written as follows to create SQL queries dynamically:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ ('Mac', 'Mohan', 20, 'M', 2000) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
Example: Following code segment is another form of execution where you can pass parameters directly:
.................................. user_id = "test123" password = "password" con.execute('insert into Login values("%s", "%s")' % \ (user_id, password)) ..................................
READ Operation on any databasse means to fetch some useful information from the database. Once our database connection is established, we are ready to make a query into this database. We can use either fetchone() method to fetch single record or fetchall() method to fetch multiple values from a database table.
Example: Following is the procedure to query all the records from EMPLOYEE table having salary more than 1000:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000) try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # Now print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data" # disconnect from server db.close()
This will produce the following result:
fname=Mac, lname=Mohan, age=20, sex=M, income=2000
UPDATE Operation on any databasse means to update one or more records, which are already available in the database. Following is the procedure to update all the records having SEX as 'M'. Here, we will increase AGE of all the males by one year. Example:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to UPDATE required records sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M') try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from EMPLOYEE where AGE is more than 20: Example:
import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to DELETE required records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
Transactions are a mechanism that ensures data consistency. Transactions should have the following four properties:
The Python DB API 2.0 provides two methods to either commit or rollback a transaction. Example: You already have seen how we have implemented transactions. Here is again similar example:
# Prepare SQL query to DELETE required records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback()
Commit is the operation, which gives a green signal to database to finalize the changes, and after this operation, no change can be reverted back. Here is a simple example to call commit method. db.commit()
If you are not satisfied with one or more of the changes and you want to revert back those changes completely, then use rollback()method. Here is a simple example to call rollback()method. db.rollback()
To disconnect Database connection, use close() method. db.close()
If the connection to a database is closed by the user with the close() method, any outstanding transactions are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly.
There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already canceled or finished statement handle. The DB API defines a number of errors that must exist in each database module. The following table lists these exceptions.
Exception | Description |
---|---|
Warning | Used for non-fatal issues. Must subclass StandardError. |
Error | Base class for errors. Must subclass StandardError. |
InterfaceError | Used for errors in the database module, not the database itself. Must subclass Error. |
DatabaseError | Used for errors in the database. Must subclass Error. |
DataError | Subclass of DatabaseError that refers to errors in the data. |
OperationalError | Subclass of DatabaseError that refers to errors such as the loss of a connection to the database. These errors are generally outside of the control of the Python scripter. |
IntegrityError | Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys. |
InternalError | Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active. |
ProgrammingError | Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you. |
NotSupportedError | Subclass of DatabaseError that refers to trying to call unsupported functionality. |
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.
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).
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.
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.
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.
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.
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. ......................
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.
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'
You can raise exceptions in several ways by using the raise statement. The general syntax for the raise statement.
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
try: Business Logic here... except "Invalid level!": Exception handling here... else: Rest of the code here...
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.
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.
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?
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.
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.
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')
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.
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.
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.
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.
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.
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.
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.
Now let's move on to some terminologies and their definitions before creating our own classes and objects.
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.
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.
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'
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__': }
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
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.
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
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
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) |
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)
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
Note: This lesson is not complete, we only released it for your benefit because there is still a lot of information in it.
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
This function attempts to match RE pattern to string with optional flags. Here is the syntax for this function: re.match(pattern, string, flags=0) Here is the description of the parameters:
Parameter | Description |
---|---|
pattern | This is the regular expression to be matched. |
string | This is the string, which would be searched to match the pattern at the beginning of string. |
flags | You can specify different flags using bitwise OR (|).These are modifiers, which are listed in the table below. |
The re.match function returns a match object on success, None on failure. We would use group(num) or groups() function of match object to get matched expression.
Match Object Methods Description group(num=0) This method returns entire match (or specific subgroup num) groups() This method returns all matching subgroups in a tuple (empty if there weren't any)
import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!" When the above code is executed, it produces following result: matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
This function searches for first occurrence of RE pattern within string with optional flags. Here is the syntax for this function:
re.search(pattern, string, flags=0)
Here is the description of the parameters:
Parameter Description pattern This is the regular expression to be matched. string This is the string, which would be searched to match the pattern anywhere in the string. flags You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.
The re.search function returns a match object on success, None on failure. We would use group(num) or groups() function of match object to get matched expression.
Match Object Methods Description group(num=0) This method returns entire match (or specific subgroup num) groups() This method returns all matching subgroups in a tuple (empty if there weren't any)
Example:
import re line = "Cats are smarter than dogs"; searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj: print "searchObj.group() : ", searchObj.group() print "searchObj.group(1) : ", searchObj.group(1) print "searchObj.group(2) : ", searchObj.group(2) else: print "Nothing found!!" When the above code is executed, it produces following result: matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default). Example:
import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print "search --> searchObj.group() : ", searchObj.group() else: print "Nothing found!!" When the above code is executed, it produces the following result: No match!! search --> matchObj.group() : dogs
Some of the most important re methods that use regular expressions is sub.
re.sub(pattern, repl, string, max=0)
This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method would return modified string. Example: Following is the example:
import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print "Phone Num : ", num # Remove anything other than digits num = re.sub(r'\D', "", phone) print "Phone Num : ", num When the above code is executed, it produces the following result: Phone Num : 2004-959-559 Phone Num : 2004959559
Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these:
Modifier Description re.I Performs case-insensitive matching. re.L Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B). re.M Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string). re.S Makes a period (dot) match any character, including a newline. re.U Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B. re.X Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.
Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \),
all characters match themselves. You can escape a control character by preceding it with a backslash. Following table lists the regular expression syntax that is available in Python:
Pattern Description ^ Matches beginning of line. $ Matches end of line. . Matches any single character except newline. Using m option allows it to match newline as well. [...] Matches any single character in brackets. [^...] Matches any single character not in brackets re* Matches 0 or more occurrences of preceding expression. re+ Matches 1 or more occurrence of preceding expression. re? Matches 0 or 1 occurrence of preceding expression. re{ n} Matches exactly n number of occurrences of preceding expression. re{ n,} Matches n or more occurrences of preceding expression. re{ n, m} Matches at least n and at most m occurrences of preceding expression. a| b Matches either a or b. (re) Groups regular expressions and remembers matched text. (?imx) Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected. (?-imx) Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected. (?: re) Groups regular expressions without remembering matched text. (?imx: re) Temporarily toggles on i, m, or x options within parentheses. (?-imx: re) Temporarily toggles off i, m, or x options within parentheses. (?#...) Comment. (?= re) Specifies position using a pattern. Doesn't have a range. (?! re) Specifies position using pattern negation. Doesn't have a range. (?> re) Matches independent pattern without backtracking. \w Matches word characters. \W Matches nonword characters. \s Matches whitespace. Equivalent to [\t\n\r\f]. \S Matches nonwhitespace. \d Matches digits. Equivalent to [0-9]. \D Matches nondigits. \A Matches beginning of string. \Z Matches end of string. If a newline exists, it matches just before newline. \z Matches end of string. \G Matches point where last match finished. \b Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. \B Matches nonword boundaries. \n, \t, etc. Matches newlines, carriage returns, tabs, etc. \1...\9 Matches nth grouped subexpression. \10 Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code.
Example Description python Match "python".
Example Description [Pp]ython Match "Python" or "python" rub[ye] Match "ruby" or "rube" [aeiou] Match any one lowercase vowel [0-9] Match any digit; same as [0123456789] [a-z] Match any lowercase ASCII letter [A-Z] Match any uppercase ASCII letter [a-zA-Z0-9] Match any of the above [^aeiou] Match anything other than a lowercase vowel [^0-9] Match anything other than a digit
Example Description . Match any character except newline \d Match a digit: [0-9] \D Match a nondigit: [^0-9] \s Match a whitespace character: [ \t\r\n\f] \S Match nonwhitespace: [^ \t\r\n\f] \w Match a single word character: [A-Za-z0-9_] \W Match a nonword character: [^A-Za-z0-9_]
Example Description ruby? Match "rub" or "ruby": the y is optional ruby* Match "rub" plus 0 or more ys ruby+ Match "rub" plus 1 or more ys \d{3} Match exactly 3 digits \d{3,} Match 3 or more digits \d{3,5} Match 3, 4, or 5 digits
This matches the smallest number of repetitions:
Example Description <.*> Greedy repetition: matches "perl>" <.*?> Nongreedy: matches "" in "perl>"
Example Description \D\d+ No group: + repeats \d (\D\d)+ Grouped: + repeats \D\d pair ([Pp]ython(, )?)+ Match "Python", "Python, python, python", etc.
This matches a previously matched group again:
Example Description ([Pp])ython&\1ails Match python&pails or Python&Pails (['"])[^\1]*\1 Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc.
Example Description python|perl Match "python" or "perl" rub(y|le)) Match "ruby" or "ruble" Python(!+|\?) "Python" followed by one or more ! or one ?
This needs to specify match position.
Example Description ^Python Match "Python" at the start of a string or internal line Python$ Match "Python" at the end of a string or line \APython Match "Python" at the start of a string Python\Z Match "Python" at the end of a string \bPython\b Match "Python" at a word boundary \brub\B \B is nonword boundary: match "rub" in "rube" and "ruby" but not alone Python(?=!) Match "Python", if followed by an exclamation point Python(?!!) Match "Python", if not followed by an exclamation point
Example Description R(?#comment) Matches "R". All the rest is a comment R(?i)uby Case-insensitive while matching "uby" R(?i:uby) Same as above rub(?:y|le)) Group only without creating \1 backreference
Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connection-less protocols. Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. This tutorial gives you understanding on most famous concept in Networking - Socket Programming What is Sockets? Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. Sockets have their own vocabulary:
Term | Description |
---|---|
Domain | The family of protocols that will be used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on. |
Type | The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols. |
Protocol | Typically zero, this may be used to identify a variant of a protocol within a domain and type. |
Hostname | The identifier of a network interface:
|
Port | Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a string containing a port number, or the name of a service. |
To create a socket, you must use the socket.socket() function available in socket module, which has the general syntax: s = socket.socket (socket_family, socket_type, protocol=0) Here is the description of the parameters:
Once you have socket object, then you can use required functions to create your client or server program. Following is the list of functions required:
Method | Description |
---|---|
s.bind() | This method binds address (hostname, port number pair) to socket. |
s.listen() | This method sets up and start TCP listener. |
s.accept() | This passively accept TCP client connection, waiting until connection arrives (blocking). |
Method | Description |
---|---|
s.connect() | This method actively initiates TCP server connection. |
Method | Description |
---|---|
s.recv() | This method receives TCP message |
s.send() | This method transmits TCP message |
s.recvfrom() | This method receives UDP message |
s.sendto() | This method transmits UDP message |
s.close() | This method closes socket |
s.gethostname() | Returns the host-name. |
Where s = socket.socket() after importing the socket module with import socket
To write Internet servers, we use the socketfunction available in socket module to create a socket object. A socket object is then used to call other functions to setup a socket server. Now call bind(hostname, port function to specify a port for your service on the given host. Next, call the accept method of the returned object. This method waits until a client connects to the port you specified, and then returns a connection object that represents the connection to that client.
# This is server.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') c.close() # Close the connection
Now we will write a very simple client program which will open a connection to a given port 12345 and given host. This is very simple to create a socket client using Python's socketmodule function. The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file. The following code is a very simple client that connects to a given host and port, reads any available data from the socket, and then exits:
# This is client.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done
Now run this server.py
in background and then run above client.py to see the result.
# Following would start a server in background. $ python server.py & # Once server is started run client as follows: $ python client.py
This would produce following result:
Got connection from ('127.0.0.1', 48437) Thank you for connecting Python Internet modules
A list of some important modules which could be used in Python Network/Internet programming.
Protocol | Common function | PORT number | PYTHON MODULE |
---|---|---|---|
HTTP | Web pages | 80 | httplib, urllib, xmlrpclib |
NNTP | Usenet news | 119 | nntplib |
FTP | File transfers | 20 | ftplib, urllib |
SMTP | Sending email | 25 | smtplib |
POP3 | Fetching email | 110 | poplib |
IMAP4 | Fetching email | 143 | imaplib |
Telnet | Command lines | 23 | telnetlib |
Gopher | Document transfers | 70 | gopherlib, urllib |
Please check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.
Running several threads is similar to running several different programs concurrently, but with the following benefits:
A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track of where within its context it is currently running.
To spawn another thread, you need to call following method available in thread module: thread.start_new_thread ( function, args[, kwargs] ) This method call enables a fast and efficient way to create new threads in both Linux and Windows. The method call returns immediately and the child thread starts and calls function with the passed list of agrs. When function returns, the thread terminates. Here, args is a tuple of arguments; use an empty tuple to call function without passing any arguments. kwargs is an optional dictionary of keyword arguments. Example:
import thread import time # Define a function for the thread def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s" % ( threadName, time.ctime(time.time()) ) # Create two threads as follows try: thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) ) except: print "Error: unable to start thread" while 1: pass
When the above code is executed, it produces the following result:
Thread-1: Thu Jan 22 15:42:17 2009 Thread-1: Thu Jan 22 15:42:19 2009 Thread-2: Thu Jan 22 15:42:19 2009 Thread-1: Thu Jan 22 15:42:21 2009 Thread-2: Thu Jan 22 15:42:23 2009 Thread-1: Thu Jan 22 15:42:23 2009 Thread-1: Thu Jan 22 15:42:25 2009 Thread-2: Thu Jan 22 15:42:27 2009 Thread-2: Thu Jan 22 15:42:31 2009 Thread-2: Thu Jan 22 15:42:35 2009
Although it is very effective for low-level threading, but the thread module is very limited compared to the newer threading module.
The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section. The threading module exposes all the methods of the thread module and provides some additional methods:
In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Threadclass are as follows:
To implement a new thread using the threading module, you have to do the following:
Once you have created the new Threadsubclass, you can create an instance of it and then start a new thread by invoking the start(), which will in turn call run() method. Example:
import threading import time exitFlag = 0 class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Starting " + self.name print_time(self.name, self.counter, 5) print "Exiting " + self.name def print_time(threadName, delay, counter): while counter: if exitFlag: thread.exit() time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 # Create new threads thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # Start new Threads thread1.start() thread2.start() print "Exiting Main Thread" When the above code is executed, it produces the following result: Starting Thread-1 Starting Thread-2 Exiting Main Thread Thread-1: Thu Mar 21 09:10:03 2013 Thread-1: Thu Mar 21 09:10:04 2013 Thread-2: Thu Mar 21 09:10:04 2013 Thread-1: Thu Mar 21 09:10:05 2013 Thread-1: Thu Mar 21 09:10:06 2013 Thread-2: Thu Mar 21 09:10:06 2013 Thread-1: Thu Mar 21 09:10:07 2013 Exiting Thread-1 Thread-2: Thu Mar 21 09:10:08 2013 Thread-2: Thu Mar 21 09:10:10 2013 Thread-2: Thu Mar 21 09:10:12 2013 Exiting Thread-2
The threading module provided with Python includes a simple-to-implement locking mechanism that will allow you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method of the new lock object would be used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread will wait to acquire the lock. If blocking is set to 0, the thread will return immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread will block and wait for the lock to be released. The release() method of the the new lock object would be used to release the lock when it is no longer required. Example:
import threading import time class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Starting " + self.name # Get lock to synchronize threads threadLock.acquire() print_time(self.name, self.counter, 3) # Free lock to release next thread threadLock.release() def print_time(threadName, delay, counter): while counter: time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 threadLock = threading.Lock() threads = [] # Create new threads thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # Start new Threads thread1.start() thread2.start() # Add threads to thread list threads.append(thread1) threads.append(thread2) # Wait for all threads to complete for t in threads: t.join() print "Exiting Main Thread" When the above code is executed, it produces the following result: Starting Thread-1 Starting Thread-2 Thread-1: Thu Mar 21 09:11:28 2013 Thread-1: Thu Mar 21 09:11:29 2013 Thread-1: Thu Mar 21 09:11:30 2013 Thread-2: Thu Mar 21 09:11:32 2013 Thread-2: Thu Mar 21 09:11:34 2013 Thread-2: Thu Mar 21 09:11:36 2013 Exiting Main Thread
The Queue module allows you to create a new queue object that can hold a specific number of items. There are following methods to control the Queue:
Example:
import Queue import threading import time exitFlag = 0 class myThread (threading.Thread): def __init__(self, threadID, name, q): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.q = q def run(self): print "Starting " + self.name process_data(self.name, self.q) print "Exiting " + self.name def process_data(threadName, q): while not exitFlag: queueLock.acquire() if not workQueue.empty(): data = q.get() queueLock.release() print "%s processing %s" % (threadName, data) else: queueLock.release() time.sleep(1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock() workQueue = Queue.Queue(10) threads = [] threadID = 1 # Create new threads for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1 # Fill the queue queueLock.acquire() for word in nameList: workQueue.put(word) queueLock.release() # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit exitFlag = 1 # Wait for all threads to complete for t in threads: t.join() print "Exiting Main Thread" When the above code is executed, it produces the following result: Starting Thread-1 Starting Thread-2 Starting Thread-3 Thread-1 processing One Thread-2 processing Two Thread-3 processing Three Thread-1 processing Four Thread-2 processing Five Exiting Thread-3 Exiting Thread-1 Exiting Thread-2 Exiting Main Thread
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail:
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
Here is the detail of the parameters:
An SMTP object has an instance method called sendmail, which will typically be used to do the work of mailing a message. It takes three parameters:
Example: Here is a simple way to send one e-mail using Python script. Try it once:
import smtplib sender = 'from@fromdomain.com' receivers = ['to@todomain.com'] message = """From: From Person To: To Person Subject: SMTP e-mail test This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and Subject header, separated from the body of the e-mail with a blank line. To send the mail you use smtpObj to connect to the SMTP server on the local machine and then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail). If you're not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you're using a webmail service (such as Hotmail or Yahoo! Mail), your e-mail provider will have provided you with outgoing mail server details that you can supply them, as follows: smtplib.SMTP('mail.your-domain.com', 25)
When you send a text message using Python, then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But Python provides option to send an HTML message as actual HTML message. While sending an e-mail message, you can specify a Mime version, content type and character set to send an HTML e-mail. Example: Following is the example to send HTML content as an e-mail. Try it once:
import smtplib message = """From: From Person To: To Person MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format This is HTML message.
""" try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
To send an e-mail with mixed content requires to set Content-type header to multipart/mixed. Then, text and attachment sections can be specified within boundaries. A boundary is started with two hyphens followed by a unique number, which can not appear in the message part of the e-mail. A final boundary denoting the e-mail's final section must also end with two hyphens. Attached files should be encoded with the pack("m") function to have base64 encoding before transmission. Example: Following is the example, which will send a file /tmp/test.txt as an attachment. Try it once:
import smtplib import base64 filename = "/tmp/test.txt" # Read a file and encode it into base64 format fo = open(filename, "rb") filecontent = fo.read() encodedcontent = base64.b64encode(filecontent) # base64 sender = 'webmaster@tutorialpoint.com' reciever = 'amrood.admin@gmail.com' marker = "AUNIQUEMARKER" body =""" This is a test email to send an attachement. """ # Define the main headers. part1 = """From: From Person To: To Person Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # Define the message action part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # Define the attachment section part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3 try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, reciever, message) print "Successfully sent email" except Exception: print "Error: unable to send email"
Python provides various options for developing graphical user interfaces (GUIs). Most important are listed below:
There are many other interfaces available which I'm not listing here. You can find them over the net.
Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps:
Example:
import Tkinter top = Tkinter.Tk() # Code to add widgets will go here... top.mainloop()
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets. There are currently 15 types of widgets in Tkinter. We present these widgets as well as a brief description in the following table:
Let's take a look at how some of their common attributes.such as sizes, colors and fonts are specified.
All Tkinter widgets have access to specific geometry management methods, which have the purpose of organizing widgets throughout the parent widget area. Tkinter exposes the following geometry manager classes: pack, grid, and place.
Any code that you write using any compiled language like C, C++ or Java can be integrated or imported into another Python script. This code is considered as an "extension." A Python extension module is nothing more than a normal C library. On Unix machines, these libraries usually end in .so (for shared object). On Windows machines, you typically see .dll (for dynamically linked library). Pre-Requisite: To start writing your extension, you are going to need the Python header files.
Additionally, it is assumed that you have good knowledge of C or C++ to write any Python Extension using C programming. First look at a Python extension: For your first look at a Python extension module, you'll be grouping your code into four parts:
Start including Python.h header file in your C source file, which will give you access to the internal Python API used to hook your module into the interpreter. Be sure to include Python.h before any other headers you might need. You'll follow the includes with the functions you want to call from Python.
The signatures of the C implementations of your functions will always take one of the following three forms:
static PyObject *MyFunction( PyObject *self, PyObject *args ); static PyObject *MyFunctionWithKeywords(PyObject *self, PyObject *args, PyObject *kw); static PyObject *MyFunctionWithNoArgs( PyObject *self );
Each one of the preceding declarations returns a Python object. There's no such thing as a void function in Python as there is in C. If you don't want your functions to return a value, return the C equivalent of Python's None value. The Python headers define a macro, Py_RETURN_NONE, that does this for us. The names of your C functions can be whatever you like as they will never be seen outside of the extension module. So they would be defined as static function. Your C functions usually are named by combining the Python module and function names together, as shown here:
static PyObject *module_func(PyObject *self, PyObject *args) { /* Do your stuff here. */ Py_RETURN_NONE; }
This would be a Python function called func inside of the module module. You'll be putting pointers to your C functions the method table for the module that usually comes next in your source code. The method mapping table: This method table is a simple array of PyMethodDef structures. That structure looks something like this:
struct PyMethodDef { char *ml_name; PyCFunction ml_meth; int ml_flags; char *ml_doc; };
Here is the description of the members of this structure:
This table needs to be terminated with a sentinel that consists of NULL and 0 values for the appropriate members. Example: For the above-defined function, we would have following method mapping table:
static PyMethodDef module_methods[] = { { "func", (PyCFunction)module_func, METH_NOARGS, NULL }, { NULL, NULL, 0, NULL } };
The last part of your extension module is the initialization function. This function is called by the Python interpreter when the module is loaded. It's required that the function be named initModule, where Module is the name of the module. The initialization function needs to be exported from the library you'll be building. The Python headers define PyMODINIT_FUNC to include the appropriate incantations for that to happen for the particular environment in which we're compiling. All you have to do is use it when defining the function. Your C initialization function generally has the following overall structure:
PyMODINIT_FUNC initModule() { Py_InitModule3(func, module_methods, "docstring...");
Here is the description of Py_InitModule3function:
Putting this all together looks like the following:
#include static PyObject *module_func(PyObject *self, PyObject *args) { /* Do your stuff here. */ Py_RETURN_NONE; } static PyMethodDef module_methods[] = { { "func", (PyCFunction)module_func, METH_NOARGS, NULL }, { NULL, NULL, 0, NULL } }; PyMODINIT_FUNC initModule() { Py_InitModule3(func, module_methods, "docstring..."); }
Example: A simple example that makes use of all the above concepts:
#include static PyObject* helloworld(PyObject* self) { return Py_BuildValue("s", "Hello, Python extensions!!"); } static char helloworld_docs[] = "helloworld( ): Any message you want to put here!!\n"; static PyMethodDef helloworld_funcs[] = { {"helloworld", (PyCFunction)helloworld, METH_NOARGS, helloworld_docs}, {NULL} }; void inithelloworld(void) { Py_InitModule3("helloworld", helloworld_funcs, "Extension module example!"); }
Here the Py_BuildValue function is used to build a Python value. Save above code in hello.c file. We would see how to compile and install this module to be called from Python script.
The distutils package makes it very easy to distribute Python modules, both pure Python and extension modules, in a standard way. Modules are distributed in source form and built and installed via a setup script usually called setup.py as follows. For the above module, you would have to prepare following setup.py script: from distutils.core import setup, Extension
setup(name='helloworld', version='1.0', \ ext_modules=[Extension('helloworld', ['hello.c'])])
Now, use the following command, which would perform all needed compilation and linking steps, with the right compiler and linker commands and flags, and copies the resulting dynamic library into an appropriate directory: $ python setup.py install On Unix-based systems, you'll most likely need to run this command as root in order to have permissions to write to the site-packages directory. This usually isn't a problem on Windows
Once you installed your extension, you would be able to import and call that extension in your Python script as follows:
import helloworld print helloworld.helloworld()
This would produce the following result:
Hello, Python extensions!!
Because you'll most likely want to define functions that do accept arguments, you can use one of the other signatures for your C functions. For example, following function, that accepts some number of parameters, would be defined like this:
static PyObject *module_func(PyObject *self, PyObject *args) { /* Parse args and do something interesting here. */ Py_RETURN_NONE; }
The method table containing an entry for the new function would look like this:
static PyMethodDef module_methods[] = { { "func", (PyCFunction)module_func, METH_NOARGS, NULL }, { "func", module_func, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } };
You can use API PyArg_ParseTuple function to extract the arguments from the one PyObject pointer passed into your C function. The first argument to PyArg_ParseTuple is the args argument. This is the object you'll be parsing. The second argument is a format string describing the arguments as you expect them to appear. Each argument is represented by one or more characters in the format string as follows.
static PyObject *module_func(PyObject *self, PyObject *args) { int i; double d; char *s; if (!PyArg_ParseTuple(args, "ids", &i, &d, &s)) { return NULL; } /* Do something interesting here. */ Py_RETURN_NONE; }
Compiling the new version of your module and importing it will enable you to invoke the new function with any number of arguments of any type:
module.func(1, s="three", d=2.0) module.func(i=1, d=2.0, s="three") module.func(s="three", d=2.0, i=1)
You can probably come up with even more variations.
Here is the standard signature for PyArg_ParseTuple function:
int PyArg_ParseTuple(PyObject* tuple,char* format,...)
This function returns 0 for errors, and a value not equal to 0 for success. tuple is the PyObject* that was the C function's second argument. Here format is a C string that describes mandatory and optional arguments. Here is a list of format codes for PyArg_ParseTuple function:
Code | C type | Meaning |
---|---|---|
c | char | A Python string of length 1 becomes a C char. |
d | double | A Python float becomes a C double. |
f | float | A Python float becomes a C float. |
i | int | A Python int becomes a C int. |
l | long | A Python int becomes a C long. |
L | long long | A Python int becomes a C long long |
O | PyObject* | Gets non-NULL borrowed reference to Python argument. |
s | char* | Python string without embedded nulls to C char*. |
s# | char*+int | Any Python string to C address and length. |
t# | char*+int | Read-only single-segment buffer to C address and length. |
u | Py_UNICODE* | Python Unicode without embedded nulls to C. |
u# | Py_UNICODE*+int | Any Python Unicode C address and length. |
w# | char*+int | Read/write single-segment buffer to C address and length. |
z | char* | Like s, also accepts None (sets C char* to NULL). |
z# | char*+int | Like s#, also accepts None (sets C char* to NULL). |
(...) | as per ... | A Python sequence is treated as one argument per item |
: | Format end, followed by function name for error messages. | |
; | Format end, followed by entire error message text. | |
| | The following arguments are optional. |
Py_BuildValue takes in a format string much like PyArg_ParseTuple does. Instead of passing in the addresses of the values you're building, you pass in the actual values. Here's an example showing how to implement an add function:
static PyObject *foo_add(PyObject *self, PyObject *args) { int a; int b; if (!PyArg_ParseTuple(args, "ii", &a, &b)) { return NULL; } return Py_BuildValue("i", a + b); }
This is what it would look like if implemented in Python:
def add(a, b): return (a + b)
You can return two values from your function as follows, this would be cauptured using a list in Python.
static PyObject *foo_add_subtract(PyObject *self, PyObject *args) { int a; int b; if (!PyArg_ParseTuple(args, "ii", &a, &b)) { return NULL; } return Py_BuildValue("ii", a + b, a - b); }
This is what it would look like if implemented in Python:
def add_subtract(a, b): return (a + b, a - b)
Here is the standard signature for Py_BuildValue function:
PyObject* Py_BuildValue(char* format,...)
Here format is a C string that describes the Python object to build. The following arguments of Py_BuildValue are C values from which the result is built. The PyObject* result is a new reference. Following table lists the commonly used code strings, of which zero or more are joined into string format.
Code | C type | Meaning |
---|---|---|
c | char | A C char becomes a Python string of length 1. |
d | double | A C double becomes a Python float. |
f | float | A C float becomes a Python float. |
i | int | A C int becomes a Python int. |
l | long | A C long becomes a Python int. |
N | PyObject* Passes a Python object and steals a reference. | |
O | PyObject* Passes a Python object and INCREFs it as normal. | |
O& | convert+void* | Arbitrary conversion |
s | char* | C 0-terminated char* to Python string, or NULL to None. |
s# | char*+int | C char* and length to Python string, or NULL to None. |
u | Py_UNICODE* | C-wide, null-terminated string to Python Unicode, or NULL to None. |
u# | Py_UNICODE*+int | C-wide string and length to Python Unicode, or NULL to None. |
w# | char*+int | Read/write single-segment buffer to C address and length. |
z | char* | Like s, also accepts None (sets C char* to NULL). |
z# | char*+int | Like s#, also accepts None (sets C char* to NULL). |
(...) | as per ... | Builds Python tuple from C values. |
[...] | as per ... | Builds Python list from C values. |
{...} | as per ... | Builds Python dictionary from C values, alternating keys and values. |
Code
{...} builds dictionaries from an even number of C values, alternately keys and values. For example, Py_BuildValue("{issi}",23,"zig","zag",42) returns a dictionary like Python's {23:'zig','zag':42}.
The standard library comes with a number of modules that can be used both as modules and as command-line utilities. The dis Module: The dis module is the Python disassembler. It converts byte codes to a format that is slightly more appropriate for human consumption. You can run the disassembler from the command line. It compiles the given script and prints the disassembled byte codes to the STDOUT. You can also use dis as a module. The dis function takes a class, method, function or code object as its single argument. Example:
import dis def sum(): vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum # Call dis function for the function. dis.dis(sum)
This would produce the following result:
6 0 LOAD_CONST 1 (10) 3 STORE_FAST 0 (vara) 7 6 LOAD_CONST 2 (20) 9 STORE_FAST 1 (varb) 9 12 LOAD_FAST 0 (vara) 15 LOAD_FAST 1 (varb) 18 BINARY_ADD 19 STORE_FAST 2 (sum) 10 22 LOAD_CONST 3 ('vara + varb = %d') 25 LOAD_FAST 2 (sum) 28 BINARY_MODULO 29 PRINT_ITEM 30 PRINT_NEWLINE 31 LOAD_CONST 0 (None) 34 RETURN_VALUE
The pdb module is the standard Python debugger. It is based on the bdb debugger framework. You can run the debugger from the command line (type n [or next] to go to the next line and help to get a list of available commands): Example: Before you try to run pdb.py, set your path properly to Python lib directory. So let us try with above example sum.py: $pdb.py sum.py
> /test/sum.py(3)() -> import dis (Pdb) n > /test/sum.py(5)() -> def sum(): (Pdb) n >/test/sum.py(14)() -> dis.dis(sum) (Pdb) n 6 0 LOAD_CONST 1 (10) 3 STORE_FAST 0 (vara) 7 6 LOAD_CONST 2 (20) 9 STORE_FAST 1 (varb) 9 12 LOAD_FAST 0 (vara) 15 LOAD_FAST 1 (varb) 18 BINARY_ADD 19 STORE_FAST 2 (sum) 10 22 LOAD_CONST 3 ('vara + varb = %d') 25 LOAD_FAST 2 (sum) 28 BINARY_MODULO 29 PRINT_ITEM 30 PRINT_NEWLINE 31 LOAD_CONST 0 (None) 34 RETURN_VALUE --Return-- > /test/sum.py(14)()->None -v dis.dis(sum) (Pdb) n --Return-- > (1)()->None (Pdb)
The profile module is the standard Python profiler. You can run the profiler from the command line: Example: Let us try to profile the following program:
vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum Now, try running cProfile.py over this file sum.pyas follows: $cProfile.py sum.py vara + varb = 30 4 function calls in 0.000 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno 1 0.000 0.000 0.000 0.000 :1() 1 0.000 0.000 0.000 0.000 sum.py:3() 1 0.000 0.000 0.000 0.000 {execfile} 1 0.000 0.000 0.000 0.000 {method ......}
The tabnanny module checks Python source files for ambiguous indentation. If a file mixes tabs and spaces in a way that throws off indentation, no matter what tab size you're using, the nanny complains: Example: Let us try to profile the following program:
vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum
If you would try a correct file with tabnanny.py, then it won't complain as follows:
$tabnanny.py -v sum.py 'sum.py': Clean bill of health.
There exist a lot of frameworks for Python programming that you can utilise to make your work easier. Search the internet for modules before writing your own in order to cut short the time spent developing. Other programmers have made available many utilities in the forms of frameworks, modules and extensions. Such frameworks, extensions, modules.., include; to mention but a few:
These are but a few of the great tools available.Read on these to extend your programming experience in Python.
Thank you for journeying with PywE. We will like to get some feedback from you in order to improve our future courses and even the one you took for those coming in. Send us feedback>>> .