PYTHON BASICS
PYTHON BASICS
1.INTRODUCTION
2.PYTHON DRAWBACK
3.PYTHON BLOCK STRUCTURE
4.DYNAMIC NATURE
5.OPERATORS
6.STRINGS
7.LIST
8.REFERENCES
INTRODUCTION
Why python ?
Python is a powerful programming language.
Python is a dynamic typed programming language.
Python is user friendly syntax
In python we have supporting libraries.
We can use python in GUI,Web
development,scripting,Image processing,Machine
learning .
Contd..
Who uses python ?
Google ,youtube,dropbox storage service,
Raspberry pi embedded board systems,
Intel,Cisco,HP,IBM
PYTHON DRAWBACK
Python does not communicate directly with hardware.
Python execution speed is low compared to c,
c++.
c language will directly communicate with hardware.
C++ also directly communicate with hardware
python communicate intermediately and converts in to low
level language and communicate with hardware .
PYTHON BLOCK STRUCTURE
C , C++ ,JAVA use curly braces to group the statements.
Python uses Identation.
Example:
print(“group1”)
print(“group2”)
if True:
print(“group3”)
print(“group3”)
print(“group2”)
print(“group1”)
Comments in python
N = int (input(“enter a number”))
#if N is negative returns minus of N
if N<0:
N=-N
print(N)
PRINT FUNCTION
Python command print – displays data,values and expressions.
The single and double quotation mark string objects,which are
collections of texts surrounded by quotes.
print(“ ”)
print(‘ ‘)
print(10) // print integer
print(“hello”) // print string
print(“hi i am ‘satish’ ”)
print(‘brain ”wash” ’)
ESCAPE SEQUENCE
It will optimize repeatative task
print(“happy new year”)
print(“happy\tnew\tyear”)
print(“hello\nworld”)
print(‘hello\ ‘world\’ ’)
print(“hello\”world\” ”)
print(“hello\\world”)
printing Raw string:
print(‘c:\desktop\new.txt’)
output: c:\desktop
ew.txt
if we put r in print function we can avoid the above output that is next line
print(r’c:\desktop\new.txt)
NEED OF VARIABLES
Valid variable names
rules:
1.start with letter a-z or underscore
valid invalid
age 1age
_age
Age
no special characters allowed other than
underscore(_)
Contd..
Valid
_age
age_
invalid:
age_*
+age
variable names are case sensitive
age is not equal to Age
variable name should not be a python keyword
ex:- pass , break , continue
Contd..
Dynamic nature :
statically typed programming languages are
c,c++,java
a=10
objects
b=a
System table Type = integer
a Value = 10
Count =2
others
1024
Contd..
Dynamic nature :
statically typed programming languages are
c,c++,java
a=10
objects
b=a
System table Type = integer
a Value = 10
Count =2
others
1024
Contd..
Object are garbage collected
i=10
i=hello
i=5.9
ID function :-
id function return object identity
Printing the id
a=10
print(id(a)) // the value is 1024
Identity operator
To check the two objects share same memory location we use identity operator.
In python 2 types of identity operators are there:
1.is operator
2.is not operator
syntax: obj1 is obj2
x=10 , y=10
if x is y :
print(“same”)
else :
print(“different”)
Contd..
2. is not operator
syntax :obj1 is not obj2
x=10
y=20
if x is not y :
print(“different”)
else:
print(“same”)
Printing value of a variables and string formatting
name = “python”
year = 2021
print(“name:”,name, “year :”,year)
print(“Name:”+name+”year:”+year)
above statement will be type error
In python we cannot concatenate string and integer then we go for typecast
now use the type cast
print(“Name :” +name+ “year: ” +str(year)”)
use string format
name = “python”
year = 2021
print(“name:{ } year:{ } ”.format(name,year))
STRINGS
An ordered collection of characters to store and represent text-based
information.
Python strings are categorised as immutable sequences means they have a
left-to-right order(sequence) and cannot be changed in place(immutable)
str= ”hello”
print(str)
python string index:
string indexing in python
str = “python”
print(“1st character ”,str[0])
print(“4th character ”,str[3])
FORWARD INDEXING
0 1 2 3 4 5
P Y T H O N
-5 -4 -3 -1 Reverse indexing
-6 -2
Str = “python”
Str[2] = ‘z’
String is immutable
Values can’t be changed
SLICING STRING IN PYTHON
String[start index : end index]
Str = “python”
Print (str[0:3])
print(str[-4:-1])
Forward index
0 1 2 3 4 5
P y t h o n
-6 -5 -4 -3 -2 -1
Reverse index
Slicing with default values :
List[0:index]
List[index:length of the string]
Str = “python”
print(str[:3]) // str[0:3]
print(str[3:]) // str[3:6]
String methods
Length of a string :
str = “python”
print(len(str))
replace the string:-
str = “python”
new_str= str.replace(“t”,”T”)
print(new_str)
REMOVE WHITESPACE FROM THE BEGINNING OR THE END
str = “ python ”
print(str.strip())
change string to uppercase :-
str = “python”
print(str.upper())
change string to lower case:-
str = ‘PYTHON”
print(str.lower())
Split the string
Str = “python,scripting”
print(str.split(“,”))
here where comma(,) is there that is for
splitting.
Str = “python-scripting-language”
print(str.split(“_”))
LIST
List is a collection of things in order
syntax:
variable = [item1,item2,itemN]
colors = [“red”,”blue”,’green”]
numbers = [10,20,30,40,50]
print(numbers)
data = [“python”,3.64 , 2021]
print(data)
printing list items using for loop:
numbers = [10,20,30,40,50]
for x in numbers:
print(x)
Contd..
Check if items exits
Numbers = [10,20,30,40,50]
if 10 in numbers:
print(“yes”)
else :
print(“ no ”)
python list index:
numbers = [10,20,30,40,50]
print(“1st number”,numbers[0])
print(“4th number”,numbers[3])
print(“100 number”,numbers[100]) // error will raise list index out of range
Contd..
Numbers = [1,2,3,4,5]
print(“last element ”,number[-1])
print(“2nd last element”,number[-2])
Replacing the list elements:-
numbers = [10,20,30,40,50]
print(numbers)
numbers[2]=100
print(numbers)
Contd..
Slicing list in python:-
slicing means dividing the things
list[start index:end index]
numbers = [10,20,30,40,50]
print(numbers[0:3])
print(numbers[-4:-1])
slicing with default values:-
list[0:index]
list[index:length of the list]
numbers= [10,20,30,40,50]
print(numbers[:3])
print(numbers[3:])
Contd..
Assignment to slices:
numbers=[10,20,30,40,50]
numbers[0:3]=[-1,-2,-3]
print(numbers)
Removing elements from the list:
numbers = [10,20,30,40,50]
numbers[1:3] = [ ]
print(numbers)
Copy of the list:
numbers = [1,2,3,4,5]
numbers_copy=numbers[:]
print(“copy of the list”,numbers_copy)
Contd..
Clear the whole list :-
numbers = [1,2,3,4,5]
numbers[:] = [ ]
print(numbers)
LIST METHODS:-
Length of a list:
len(list)
numbers = [10,20,30,40]
print(len(numbers))
Contd..
Concatenating lists:
a=[10,20,30]
b= [40,50]
c=a+b
print(c)
Appending items to list:
syntax:
list.append(item)
numbers = [10,20,30,40]
numbers.append(50)
print(numbers)
INSERTING ITEMS TO LIST:
list.insert(index,item)
numbers = [10,20,30,40]
numbers.insert(0,100)
print(numbers)
Removing item from a list
List.remove(item)
numbers=[10,20,30,40]
numbers.remove(10)
print(numbers)
Removing unknown item:
numbers = [10,20,30,40]
numbers.remove(100) // value error: list.remove(x):x not in list
Removing item using del keyword:
del list[index]
numbers = [10,20,30,40]
del numbers[0]
print(numbers)
Contd..
Clear a list:
list.clear()
numbers = [10,20,30,40]
numbers.clear()
print(numbers)
Delete the list:
numbers = [10,20,30,40]
del numbers
print(numbers) // name error: name ‘numbers ’ is not defined
REFERENCES
● https://www.programiz.com/python-programming
● https://www.tutorialspoint.com/python/index.htm
● https://www.geeksforgeeks.org/python-programming-language/
THANK YOU