Вы находитесь на странице: 1из 17

Introduction to Python

-Rajdeep Buragohain
What is Python
• High-level language, can do a lot with
relatively little code
• Created by Guido van Rossum and first
released in 1991.
• Guido van Rossun ws a fan of “Monty Python's
Flying Circus” so named his summer project as
“Python”.
Very Basics
 Variables
 Printing
 Operators
 Input
 Comments
Basics
• Variables:
a = int(32), b = float(32), c = “Monty Python”
• Print:
print (a), print (b), print(c)
32 32.0 Monty Python
• Operator:
Addition(+), Subtraction(-),Division(/),
Modulus(%) , Exponent(**)
Basics
• Input:
>>> i = raw_input("Enter a mathematical
expression:")
Enter a mathematical expression: 3+5
>>> print i
3+5
>>> i = input("Enter a mathematical expression:")
Enter a mathematical expression: 3+5
>>> print i
8
Basics
• Anything after a # symbol is treated as a
comment
Collection Data Types
Tuples
List
Dictionaries
Tuples & List
 Tuples are a collection of data items. They
may be of different types. Tuples are
immutable like strings. Lists are like tuples
but are mutable.
 A = (1,2,3), B =[1,2,3]

A is a tuple and B is a list.


Dictionaries
• Unordered collections where items are accessed
by a key, not by the position in the list
>>> A = {'name':"Rambo",'Age':"60",'occasion':
"Army"}
>>> A['name']
'Rambo'
>>> A['name']= "Rock"
>>> A
{'occasion': 'Army', 'Age': '60', 'name': 'Rock'}
Repetition(Loop) & Selection(if/else)
• for loop: Repeats a set of statements over a group of values.
– Syntax:
for variableName in groupOfValues:
statements

• if statement: Executes a group of statements


only if a certain condition is true. Otherwise,
the statements are skipped.
– Syntax:
if condition:
statements
For loop
• for x in range(0,6):
print x,'square is:',x**2
0 square is: 0
1 square is: 1
2 square is: 4
3 square is: 9
4 square is: 16
5 square is: 25
if/else statement
a=2
if a > 5:
print ("True")
else:
print ("False")
Ans: False
While loop
• while loop: Executes a group of statements as long as a condition is True.

– good for indefinite loops (repeat an unknown


number of times)

• Syntax:
while condition:
statements
While Loop
a=1

while a < 200:


a = a+a
print a
2
4
8
16
32
64
128
256
Function
• Functions are a convenient way to divide your
code into useful blocks, allowing us to order
our code, make it more readable, reuse it and
save some time. Also functions are a key way
to define interfaces so programmers can share
their code
Function
• def addition(a,b):
s = a+b
return s
print addition(5,3)
Class and Object

Вам также может понравиться