Python Programming Language I NCOE
Python Programming Language I NCOE
Reserved Words:
The following list shows the reserved words in Python. These reserved words may not be used as constant or
variable or any other identifier names. Keywords contain lowercase letters only.
Comments in Python:
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical
line end are part of the comment, and the Python interpreter ignores them.
# Created by Saman Perera
Operators in Python
Python language supports following type of operators.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Bitwise Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus - Divides left hand operand by right hand operand and returns remainder
** Exponent - Performs exponential (power) calculation on operators
// Floor Division - The division of operands where the result is the quotient in which the digits
after the decimal point are removed.
Python Variables
Examples:
counter = 100
miles = 1000.0
name = "Saman"
Multiple Assignments:
You can also assign a single value to several variables simultaneously. For example:
a = b = c = 10
Here 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.5, "ICT"
a,b=b,a
Python Strings:
Strings in Python are identified as a contiguous set of characters in between quotation marks.
Python allows for either pairs of single (‘) or double quotes (“).
Each character is identified by an unique integer called index starting from 0
st=’computer’
c o m p u t e r
0 1 2 3 4 5 6 7
.. … … … -4 -3 -2 -1
Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the
beginning of the string and working their way from -1 at the end.
The plus ( + ) sign is the string concatenation operator.
The asterisk ( * ) is the repetition operator.
Lists
Lists are the most versatile of Python's compound data types.
A list contains items separated by commas and enclosed within square brackets ([]).
list1=*10,5.25,4+3j,’ICT’,*4,7++
Each item is identified by a list index
Lists are mutable objects. Once we defined a list we can modify it.
Python Tuples:
A tuple is another sequence data type that is similar to the list.
A tuple consists of a number of values separated by commas enclosed within parentheses.
Tuples can be thought of as read-only lists because they cannot be updated.
Tuples are immutable objects.
data=(5,5.25,’DBMS’,*4,7+,(3,5))
Function Description
dict.get(key, default=None) For key key, returns value or default if key not in dictionary
dict.items() Returns a list of dict's (key, value) tuple pairs
bin(255) list(range(5))
bin(255)[2:] list(range(1,5))
chr(65) list(range(5,0,-1))
chr(3461) list(range(20,0,-5))
chr(0x0d85) list(range(5,1))
float('0025.3600') str(25)
float('002.536e+5') str(0b1111)
hex(255) str(2.5e+3)
hex(0b01111110) str(0b1111)[-1]*3
int('0001230') int(str(0b1111)[-1])*5
int('123',16) sum([5,6,7,2])
int('1111',2) sum({1:2,3:4})
len('computer') tuple('ICT')
len([5,6,7,9]) type(2.0)
list('ICT') type([2])
list({1:2,3:4}) type((2,))
oct(255) ord('A')
oct(0b11111) list(range(5))