Python Fundamentals
Python Fundamentals
1) Overview of Python
The Python programming language was developed in the late 1980s by Dutch programmer Guido van Rossum. The
language was not named after the large snake species but rather after the BBC comedy series Monty Python’s Flying
Circus. Guido van Rossum happens to be a fan. Just like the Linux OS, Python eventually became an open source
software development project. However, Guido van Rossum still has a central role in deciding how the language is
going to evolve. To cement that role, he has been given the title of “Benevolent Dictator for Life” by the Python
community.
Python is a versatile and easy-to-use language that was specifically designed to make programs readable and easy to
develop. Python also has a rich library making it possible to build sophisticated applications using relatively simple-
looking, small amount of code. For these reasons, Python has become a popular application development language.
2) Using Python
We use the Python language by working with the Python interpreter. Two common ways the Python interpreter can be
used are the interactive mode and the command-line mode, which are described below.
3) Python 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.
4) Displaying Texts
5) Taking Inputs
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:
(i) raw_input( )
(ii) input( )
1
5.1) The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing
newline). Here is an example program that uses it.
6) Python Keywords
Arithmetic operators ( + , - , * , / , % , ** , // )
Comparison (i.e., relational) operators ( == , != , <> , > , < , >= , <= )
Assignment operators ( = , += , -= , *= , /= , %= , **= , //= )
Bitwise operators ( & , | , ^ , ~ , << , >> )
Logical operators ( and , or , not )
Membership operators ( in , not in )
Identity operators ( is , is not )
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
2
8) Escape Characters 9) String Formatting Operator
Number data types store numeric values. They are immutable, which means once created their value never changes.
This means, changing the value of a numeric data type results in a newly allocated object. Python supports four
different numerical types:
I. int (signed integers): often called just integers or ints, are positive or negative whole numbers with no
decimal point.
II. long (long integers ): or longs, are integers of unlimited size, written like integers and followed by an
uppercase or lowercase L.
III. 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).
IV. 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.
3
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reversed() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set() apply()
delattr() help() next() setattr() buffer()
dict() hex() object() slice() coerce()
dir() id() oct() sorted() intern()