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

PYTHON TUTORIAL

INTRODUCTION
• It is a programming and scripting language. It was
created by Guido van Rossum and released in
1991.
• Works on various platforms such as windows,
linux, Mac
• program can be saved with .py extension
• If we want to print the statement in python, we
use
print(“Hello, world”)
VARIABLES
• A variable is a reserved area allocated in the
memory
• Ex: x=5 Ex: x=“awesome”
y=“Hello” print(“python is “ +x)
print(x) ->5 ->python is awesome
print(y) ->Hello
DATA TYPES
• TEXT TYPE: str SET: set,frozenset
• NUMERIC: int BOOLEAN: bool
float
complex
• SEQUENCE: list
tuple
range
• MAPPING: dict
EXAMPLES
• X=35e3 import random
Y=12E4 print(random.randrange(1,10))->3
Z=-87.7e100
Print(type(x))
Print(type(y))
Print(type(z))

• Random is a keyword that is used to get some random range


or values
ARRAYS
• ARRAY is a specific variable, which can hold
more than one value at a time.
• METHODS:
append() extend() remove()
clear() index() reverse()
copy() insert() sort()
count() pop()
Create an array of 5 integers and display the
array items

from array import *


array_num = array('i', [1,3,5,7,9])
for i in array_num:
print(i) -> 1 3 5 7 9
print("Access first three items individually")
print(array_num[0]) ->1
print(array_num[1]) ->3
print(array_num[2]) ->5
Append a new item to the end of the array

• from array import *


array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Append 11 at the end of the array:")
array_num.append(11)
print("New array: "+str(array_num))
->[1,3,5,7,9,11]
TASKS
• Write a Python program to get the
number of occurrences of a specified
element in an array.
• Write a Python program to remove a
specified item using the index from an
array.
• Write a Python program to convert an
array to an ordinary list.
STRINGS
• b = “hello world”
print(b[2:5])-> llo
print(len(a))-> 11
• a=“ HELLO, WORLD! “
print(a.strip()) ->HELLO, WORLD
print(a.lower()) ->hello, world
print(a.replace(“H”, “J”)->JELLO, WORLD
EXAMPLES
BOOLEAN:
Ex: X= “HELLO”
Y= 15
print(bool(x))->true
print(bool(y))->true

Ex: X=200
print(isinstance(x,int))->true
OPERATORS
• ARITHMETIC: +,-,*,/,%,**,//
• ASSIGNMENT: =,+=,-=,*=,/=,%=,//=,**=
• COMPARISION: ==,!=,>,<,>=,<=
• LOGICAL: and, or, not
• IDENTITY: is, is not
• MEMBERSHIP: in,not in
ARITHMETIC OPERATORS
• a=9 a=4
add= a+b
sub=a-b
mul=a*b
div1=a/b
div2=a//b
mod=a%b
Print(add) ->13
Print(sub) ->5
Print(mul) ->36
Print(div1) ->2.25
print(div2) ->2
print(mod) ->1
->Arithmetic operators performs mathematical operations like add,sub,mul,div
ASSIGNMENT OPERATORS
• Assignment Operators are used to assign the
value like (+=,-=,*=,/=,etc).
• Ex: num1=4 num2=5
res = num1+num2
res+=num1
print(“Result of + is”,res) ->
COMPARISON OPERATORS
• These operators are used to compare the
values on either side of the operand and
determine the relation between
them(==,!=,<>,>,<=,etc)
• Ex: x=4
y=5
print((‘ x>y is ’,x>y)) ->returns false
LOGICAL OPERATORS
• These operators are used for conditional
statements i.e true or false. Logical operators are
AND, OR, NOT
• AND- It returns true if both operands are true
• OR- It returns true if either of the operand is true
• NOT- It returns true if operand is false
• Ex: a=True b=False
• Print((‘ a and b is’, a and b)) ->False
• Print((‘ a or b is’, a or b)) ->True
• Print((‘not a is’, not a)) ->False
IDENTITY OPERATORS
• To compare the memory location of two objects, Identity
operators are used. The two identity operators are (is, is not).
IS: It returns true if two variables point the same object and
false otherwise.
IS NOT: It returns false if two variables point the same object
and true otherwise.
Ex: x=20 y=20
If(x is y):
print(“x & y SAME identity”)
y=30
If(x is not y):
print(“ x&y have DIFFERENT identity”)
MEMBERSHIP OPERATORS
• These operators are used to test the membership in a
sequence such as lists,strings or tuples.(in,not in)
• Ex: x=4 y=8
list=[1,2,3,4,5];
if(x in list):
print(“x is available in the given list”)
else:
Print(“x is not available in the given list”)
If(y not in list):
print(“y is not available in the given list”)
else:
Print(“y is available in the given list”)
COLLECTIONS
• LIST
• TUPLE
• SET
• DICTIONARY
LIST
• LIST is a collection which is ordered and
changeable
• Ex: thislist = [“apple”,”banana”,”cherry”]
print(thislist) ->apple, banana, cherry
print(thislist[1]) ->banana
print(thislist[2]) ->cherry
TUPLE
• Tuple is a collection which is ordered and
unchangable
Ex: x=(“a”,”b”,”c”)
y =list(x)->converting into list
y[1]= “k”
x=tuple(y)
print(x) -> “a”,”k”,”c”
SET
• SET is a collection which is unordered and
unindexed, written in {}
• Ex: thisset={“a”, “b”, “c”}
for x in thisset:
print(x) ->(“c”,”b”,”a”)
• thisset.add
• thisset.update
• thisset.remove
DICTIONARY
• Dictionary is a collection which is unordered,
changeable and indexed, written in {}
• Ex: thisdict={
“brand”:”Ford”,
“model”:”Mustang”,
“year”:1964 }
if “model” in thisdict:
print(“model is present”) -> model is present
LOOPS
IF-ELSE:
a=200
b=33
if b>a:
print(“b is greater than a”)
elif a==b:
print(“a & b are equal”)
Else:
print(“a is greater than b”)
WHILE
Ex: i=1
while i<6:
print(i)
if i==3:
break
i=i+1
FOR LOOP
• Ex: fruits=[“a”, “b”, “c”]
for x in fruits:
print(x) -> a
b
c
TASKS
• Using for loop in python
print the list=[“python”, “is”, “awesome”]as
input with else condition.

• 1
22
333
4444
55555
Thank you