Introduction to Python Programming Part-I
Dr. Harpreet Singh,
Head PG Department of Bioinformatics
Hans Raj Mahila Maha Vidyalaya Jalandhar
Programming basics
and others contain their own console window.
3
Compiling and interpreting
compile execute
interpret
output
source code
Hello.java
byte code
Hello.class
output
source code
Hello.py
4
The Python Interpreter
language
>>> 3 + 7
10
>>> 3 < 15
True
>>> 'print me'
'print me'
>>> print 'print me' print me
>>>
Expressions
6
Examples:
1 + 4 * 3
42
+ - * /
%
**
addition, subtraction/negation, multiplication, division modulus, a.k.a. remainder
exponentiation
1 + 3 * 4 is 13
(1 + 3) * 4 is 16
Integer division
7
3 | | 52 |
4 ) 14 | 27 | ) 1425 |
12 | | 135 |
2 | | 75 |
| | 54 |
| | 21 |
3 4 ) 14
12
2
43 5 ) 218
20
18
15
3
Real numbers
8
7 / 3 * | 1.2 | + | 3 / 2 |
2 * | 1.2 | + | 3 / 2 |
2.4 | | + | 3 / 2 |
2.4 | | + | 1 |
| 3.4 | | |
Math commands
8
from math import *
Command name | Description |
abs(value) | absolute value |
ceil(value) | rounds up |
cos(value) | cosine, in radians |
floor(value) | rounds down |
log(value) | logarithm, base e |
log10(value) | logarithm, base 10 |
max(value1, value2) | larger of two values |
min(value1, value2) | smaller of two values |
round(value) | nearest whole number |
sin(value) | sine, in radians |
sqrt(value) | square root |
Constant | Description |
e | 2.7182818... |
pi | 3.1415926... |
Numbers: Floating Point
a lot of digits
>>> 1.23232
1.2323200000000001
>>> print 1.23232
1.23232
>>> 1.3E7
13000000.0
>>> int(2.0) 2
>>> float(2)
2.0
Variables
x 5 gpa 3.14
x + 4 is 9
10
name = value
x = 5
gpa = 3.14
Example
>>> x = 7
>>> x 7
>>> x+7
14
>>> x = 'hello'
>>> x
'hello'
>>>
print "Message" print Expression
moves the cursor down to the next line.
print Item1, Item2, ..., ItemN
print "Hello, world!" age = 45
print "You have", 65 - age, "years until retirement"
Output:
Hello, world!
You have 20 years until retirement
12
Example: print Statement
>>> print 'hello' hello
>>> print 'hello', 'there'
hello there
input
age = input("How old are you? ")
print "Your age is", age
print "You have", 65 - age, "years until retirement"
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
14
Input: Example
print "What's your name?" name = raw_input("> ")
print "What year were you born?" birthyear = int(raw_input("> "))
print "Hi “, name, “!”, “You are “, 2016 – birthyear
% python input.py What's your name?
> Michael
What year were you born?
>1980
Hi Michael! You are 31
Repetition (loops) and Selection (if/else)
The for loop
18
for variableName in groupOfValues:
statements
for x in range(1, 6):
print x, "squared is", x * x
Output:
1 squared | is | 1 |
2 squared | is | 4 |
3 squared | is | 9 |
4 squared | is | 16 |
5 squared | is | 25 |
range
19
- the integers between start (inclusive) and stop (exclusive)
and stop (exclusive) by step
for x in range(5, 0, -1): print x
print "Blastoff!"
Output:
5
4
3
2
1
Blastoff!
Cumulative loops
20
sum = 0
for i in range(1, 11):
sum = sum + (i * i)
print "sum of first 10 squares is", sum
Output:
sum of first 10 squares is 385
if
21
if condition:
statements
gpa = 3.4
if gpa > 2.0:
print "Your application is accepted."
if/else
2
if condition:
statements
else:
statements
gpa = 1.4
if gpa > 2.0:
print "Welcome to Mars University!"
else:
print "Your application is denied."
if condition:
statements elif condition: statements
else:
statements
1
Example of If Statements
import math
x = 30
if x <= 15 : y = x + 15
elif x <= 30 :
y = x + 30
else :
y = x print ‘y = ‘,
print math.sin(y)
In file ifstatement.py
>>> import ifstatement y = 0.999911860107
>>>
In interpreter
while
23
while condition:
statements
number = 1
while number < 200: print number, number = number * 2
1 2 4 8 16 32 64 128
While Loops
x = 1
while x < 10 : print x
x = x + 1
>>> import whileloop 1
2
3
4
5
6
7
8
9
>>>
Logic
25
Operator | Example | Result |
and | 9 != 6 and 2 < 3 | True |
or | 2 == 3 or -1 < 5 | True |
not | not 7 > 0 | False |
Operator | Meaning | Example | Result |
== | equals | 1 + 1 == 2 | True |
!= | does not equal | 3.2 != 2.5 | True |
< | less than | 10 < 5 | False |
> | greater than | 10 > 5 | True |
<= | less than or equal to | 126 <= 100 | False |
>= | greater than or equal to | 5.0 >= 5.0 | True |
Loop Control Statements
break | Jumps out of the closest enclosing loop |
continue | Jumps to the top of the closest enclosing loop |
pass | Does nothing, empty statement placeholder |
More Examples For Loops
| for x in [1,7,13,2]: print x | forloop2.py | for x in range(5) : print x | | ||
| forloop1.py | |||||
| | |||||
%python forloop1.py | | % python forloop2.py 0 1 2 3 4 | ||||
1 | ||||||
7 | ||||||
13 | ||||||
2 | ||||||
| | |||||
range(N) generates a list of numbers [0,1, …, n-1]
More Data Types: Objects
Everything is an object
>>> x = 7
>>> x 7
>>> x = 'hello'
>>> x
'hello'
>>>
Numbers: Integers
>>> 132224
132224
>>> 132323 **
2
17509376329L
>>>
Numbers: Floating Point
a lot of digits
>>> 1.23232
1.2323200000000001
>>> print 1.23232
1.23232
>>> 1.3E7
13000000.0
>>> int(2.0) 2
>>> float(2)
2.0
Numbers: Complex
>>> x = 3 + 2j
>>> y = -1j
>>> x + y
(3+1j)
>>> x * y (2-3j)
String Literals
■ + is overloaded to do concatenation
>>> x = 'hello'
>>> x = x + ' there'
>>> x
'hello there'
String Literals
>>> 'I am a string' 'I am a string'
>>> "So am I!"
'So am I!'
Substrings and Methods
>>> s = '012345'
>>> s[3]
'3'
>>> s[1:4] '123'
>>> s[2:]
'2345'
>>> s[:4] '0123'
>>> s[-2]
'4'
>>> len(x) 6
>>>
str(10.3)
'10.3'
String Formatting
>>> "One, %d, three" % 2 'One, 2, three'
>>> "%d, two, %s" % (1,3)
'1, two, 3'
>>> "%s two %s" % (1, 'three') '1 two three'
>>>
References
Thanks