Python Basics
Python Basics
Python Basics
Python Basics
Contents
•Data types in python •Dictionaries
•Operators in python Exception
•Input & Output •Introduction to OOP
•Control statement •Classes
•Arrays in python •Objects
•Strings and characters in python •Interfaces
•Functions •Inheritance
•List and tuples
How to write , save and run first python program?
1. Comments
–By #
–>>> 8+9 # Addition
–>>>17
2. Python identifiers
•Identifiers – name given to a variable
•Not accept- @,$,% as identifiers
•Case sensitive
Hello & hello both are different identifiers.
•Class name always start with capital letter
Examples:
Valid identifiers Invalid Identifiers
Myname My Name(space not allowed)
My_name 3dfig( can not start with digit)
Your_name Your#name(#not allowed)
3. Reserve Words
l Python has a list of reserved words known as keywords.
l and,del,from,none,as,elif,global,nonlocal,assert,else,if,not,b
reak,except,import,or,class,false,
in,pass,continue,finally,is,raise,def,for,lambda, return
4. Variables:
Declaring a variable:
•A variable holds a value that may change
•The process of writing the variable name is called
“Declaring the variable”
•Initializing a variable:
lvariable=expression
For ex:
>>>Year=2018
>>>name=‘SPB’
>>>year
>>>name
Data Types
Standard Data Types
•6 data types
1.Numeric
2.String
3.List
4.Tuple
5.Dictionary
6.Boolean
1. Numeric
1. Numeric : integers & real numbers(fractional
numbers)
x = txt.capitalize()
print (x)
Output:
Hello, and welcome to my world
2. casefold():Converts string into lower case
Output:
hello, and welcome to my world!
3. center():Returns a centered string
txt = ”spb"
x = txt.center(20)
print(x)
Output:
spb
4. count():Returns the number of times a specified value occurs
in a string
x = txt.count(“COE")
print(x)
Output:
2
5. endswith(): Returns true if the string ends with the specified
value
x = txt.endswith(".")
print(x)
Output:
True
6. expandtabs(): Sets the tab size of the string
txt = "A\tC\tP\tC\tO\tE"
x = txt.expandtabs(5)
print(x)
Output:
A C P C O E
7. find(): Searches the string for a specified value and returns
the position of where it was found.
x = txt.find("coe")
print(x)
Output:
6
l 10. isalnum()
l Returns true if string has at least 1 character and
all characters are alphanumeric and false otherwise.
l 11. isalpha()
l Returns true if string has at least 1 character and all
characters are alphabetic and false otherwise.
l 12. isdigit()
l Returns true if the string contains only digits and false
otherwise.
l 13. islower()
l Returns true if string has at least 1 cased character
and all cased characters are in lowercase and false
otherwise.
l 14.isspace()
l Returns true if string contains only whitespace
characters and false other wise.
l16. istitle()
lReturns true if string is properly "titlecased" and false
otherwise.
l17. isupper()
l 20. lower()
l Converts all uppercase letters in string to lowercase.
l
l 24. max(str)
l Returns the max alphabetical character from the string
str.
l 25. min(str)
l Returns the min alphabetical character from the string
str.
l
l 36. title()
l Returns "titlecased" version of string, that is, all words begin
with uppercase and the rest are lowercase.
l 38. upper()
l Converts lowercase letters in string to uppercase.
List
Access Items
You access the list items by referring to the index number:
When specifying a range, the return value will be a new list with the
specified items.
The search will start at index 2 (included) and end at index 5 (not
included).
By leaving out the start value, the range will start at the
first item:
Example
This example returns the items from the beginning to
"orange":
Example
This example returns the items from "cherry" and to the end:
Example
This example returns the items from index -4 (included) to index -1
(excluded)
Example
Print the number of items in the list:
Example
Using the append() method to append an item:
The pop() method removes the specified index, (or the last item if index is
not specified):
Example
Make a copy of a list with the copy() method:
Example
Use the extend() method to add list2 at the end of list1:
list1.extend(list2)
print(list1)
Return the number of times the value "cherry" appears in the fruits
list:
x = fruits.count("cherry")
Tuple
•Also used to store sequence of items
•A tuple is a collection which is ordered and unchangeable.
•In Python tuples are written with round brackets.
But there is a workaround. You can convert the tuple into a list, change
the list, and convert the list back into a tuple.
Example
Convert the tuple into a list to be able to change it:
Example
Print the number of items in the tuple:
Example
You cannot add items to a tuple:
Example
One item tuple, remember the commma:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Remove Items
Note: You cannot remove items in a tuple.
Tuples are unchangeable, so you cannot remove items from it, but you
can delete the tuple completely:
Example
The del keyword can delete the tuple completely:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)
Difference between List & Tuple
>>> x=False
>>> type(x)
<type 'bool'>
Dictionaries
l Union
l Intersection
l Difference
l Symmetric difference
l Union:
l Performed on 2 sets returns all elements from both the sets.
l Its performed by using | operator
>>>set1=set([1,2,3,4,5])
>>> set1
set([1, 2, 3, 4, 5])
>>> set2=set([2,3,4,5,6,7,8])
>>> set2
set([2, 3, 4, 5, 6, 7, 8])
>>> print set1
set([1, 2, 3, 4, 5])
>>> print set2
set([2, 3, 4, 5, 6, 7, 8])
>>> union=set1|set2
>>> union
Set([1, 2, 3, 4, 5, 6, 7, 8])
>>> print union
set([1, 2, 3, 4, 5, 6, 7, 8])
Intersection