PYTHON WORKSHOP
S.Dhanya Abhirami
LET’S
DIVE INTO
PYTHON
1. Presence of Third Party Modules and Extensive Support Libraries :
The Python Package Index (PyPI) contains numerous third-party modules that make Python capable of interacting with most of the other languages and platforms.
Python provides a large standard library which includes areas like internet protocols, string operations, web services tools and operating system interfaces. Many high use programming tasks have already been scripted into the standard library which reduces length of code to be written significantly.
2. Open Source and Community Development:
Python language is developed under an OSI-approved open source license, which makes it free to use and distribute, including for commercial purposes.
Further, its development is driven by the community which collaborates for its code through hosting conferences and mailing lists, and provides for its numerous modules.
3. Interpreted
Unlike C and C++, python code is translated line by line to machine language.
Easier to debug.
4. Learning Ease and Support Available:
Python offers excellent readability and uncluttered simple-to-learn syntax which helps beginners to utilize this programming language. Additionally, the wide base of users and active developers has resulted in a rich internet resource bank to encourage development and the continued adoption of the language. You get numerous tutorials and documentations online.Cool!
5. User-friendly Data Structures:
Python has built-in list and dictionary data structures which can be used to construct fast runtime data structures. Further, Python also provides the option of dynamic high-level data typing which reduces the length of support code that is needed.
6. Productivity and Speed(OOP) :
Python has clean object-oriented design, provides enhanced process control capabilities, and possesses strong integration and text processing capabilities and its own unit testing framework, all of which contribute to the increase in its speed and productivity. Python is considered a viable option for building complex multi-protocol network applications.
HISTORY
sketch television show called Monty Python’s Flying Circus
PYTHON
print(“Hello World”)
C++
#include<iostream.h>
void main()
{
cout<<“Hello world”;
}
Keywords
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
Identifiers
STRING
>>>print (“My name is Dhanya”)
My name is Dhanya
>>> print (“Good morning”)
Good morning
>>>”Hey what’s up”
‘Hey what’s up’
>>>’ Python is cool.’
’ Python is cool.’
‘Good morning ma’am’
>>>print (‘Good morning ma’am’)
SyntaxError: invalid syntax
>>>print (“Good morning ma’am”)
Good morning ma’am
>>>print (‘She said “How are you?” ’)
She said “How are you?”
‘ She said “How are you?” ’
ESCAPE CHARACTER
a ‘ within ‘…’ or a “ within “…”
>>> ‘Hello! It\’s me.’
Hello! It’s me.
Python as a Calculator
>>>1+1
2
>>>10-3
7
>>>2*4
8
>>>12/4
3
>>>5%3 #remainder
2
Variables
12.2
x
14
y
x = 12.2
y = 14
100
x = 100
Numeric Expressions
Operator | Operation |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
// | Integer Division |
** | Power |
% | Remainder |
Order of Evaluation
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Parenthesis
Power
Multiplication/Division
Addition/Subtraction
Left to Right
Parenthesis
Power
Multiplication
Addition
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x
11
1 + 2 ** 3 / 4 * 5
1 + 8 / 4 * 5
1 + 2 * 5
1 + 10
11
Note 8/4 goes before 4*5 because of the left-right rule.
Data types
>>> x = 1 + 4
>>> print (x)
5
>>>x=“Dhanya”
>>>x
Dhanya
>>> y = 'hello ' + 'there'
>>> print (y)
hello there
Type Casting
>>>n=10
>>>float(n)
10.0
>>>str(n)
‘10’
Comments
MULTILINE COMMENTS
“””” this is comment line 1
this is comment line 2
this is comment line 3””””
String Operations
>>> print ('abc' + '123’)
abc123
>>> print ('Hi' * 5)
HiHiHiHiHi
>>>
RELATIONAL OPERATORS
Note: a==10 is different from a=10 (Why?)
Logical Operators
Operator | Description | Example |
and | Called Logical AND operator. If both the operands are true then then condition becomes true. | (a and b) is true. |
or | Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. | (a or b) is true. |
not | Called Logical NOT Operator. Use 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. |
MORE ON STRINGS
Branching
Syntax of if-else
if condition1:
do this
elif condition2:
do something else
else:
do this
Take care of indentation and semicolon
Iteration
To execute a block of code repeatedly we use loops.
In a loop:
condition is checked
If true, loop body is entered
Loop body is executed over and over again until condition becomes false
Once false, we exit the loop
while Loop
count = 0
print(“before while loop”)
while (count < 9):
print 'The count is:', count
count = count + 1
print “Out of while loop"
while condition:
do this
>>>range(0,10)
>>>range(10)
>>>range(0,10,2)
For generating a sequence
range( from,to,jump)
for Loop
for i in range(0,10):
print (i**2)
for alphabet in ‘Dhanya':
print ('Current Letter :',alphabet)
Come out of the loop
Go to next iteration
Don’t do anything, like a placeholder
LIST
DATA STRUCTURES
SET
TUPLE
DICTIONARY
Key-value pairs
No need of integer indices
Note key must be immutable
>>> d={1:"hi",'a':2}
>>> d['a']
>>> d[1]
>>>d.keys()
>>>d.values()
KEEP IN MIND
| MUTABLE | ORDERED | SYNTAX |
STRING | NO | YES | “a” or ‘a’ |
LIST | YES | YES | [a,b ] |
DICTIONARY | YES | NO | {key:value} |
TUPLE | NO | YES | (a,b) |
SET | YES | NO | {a,b} |
FUNCTIONS
def functionname(parameters):
… body of function
…
Now its your turn write a function to print sum of first n numbers,
Given n
Some variations
Modules
import module
from module import *