Variables in Python:
-Like C, C++ not declaring the variables with type specification
-Based assigned value type of the variable is assigned by the compiler
syntax of defining a variables in python:
my_var = 100 #integer type
my_var = "Best Python tutorial" #string type
my_var = True #boolean type
Math operation in Python:
type() method is used to return type of the variable
num1 = 100
num2 = 12.60
type(num1) #Type of this variable; integer
type(num2) #Type of this variable; float
Operators and use in python
+ for #addition (3+2 result is 5)
– for #subtraction (3-2 result is 1)
/ for #division (see bit below)
* for #multiplication (2*3 result is 6)
** for #raising to a power (2 ** 3 result is 8 (2 to the power of 3 i.e 2*2*2 ))
% for #modulo (Operator returns the reminder after division for example 3%2 result is 1 because reminder is 1)
There is difference between float division and integer division
5 / 2 #float division; result is 2 in Python 2 and 2.5 in Python 3
5 // 2 #integer division; result is 2 in Python 2 and Python 3
math operations priority and order of evaluation in python :
High priority to raising to a power operator;
Medium priority to division, multiplication and modulo operators;
Low priority to addition and subtraction operator;
For example see the below expression evaluation
result=200 - 4 ** 2 / 4 * 2
print(result)
output : 192.0
First priority : 4 ** 2 =16,
second priority : 16/4 =4 then 4*2 = 8,
thirds priority 200-8 = 192 ;
result is 192.0
Type conversion between numeric types of data in python:
For type conversion between numeric types of data we are using int() , float() functions to convert int to float vice versa
int(3.5) #result is 3
float(20) #result is 20.0
Math functions in python:
abs() -> Return the absolute value that is positive value for negative values also it give positive value :
abs(7) result is 7
print(abs(-5.7)) result is 5.7
max() -> Return the largest values
max(5, 7) result is 7
min() -> Return the small value
min(5, 7) result is 5
pow()-> Return the power calculated value
pow(2, 3) result is 8 (other way of raising to a power operator)
Boolean and logical operators in python:
Boolean operators return either True or False for given expression.
and : and operator return true if both operands evaluated as true in the expression.
print((3 == 3) and (2 == 2)) returns True.
3==3 is true , 2==2 is true
print((3 == 3) and (3 == 2)) returns False.
3==3 is true,3 == 2 false
or: or operator return true if at least one operand evaluated as true in both operands.
print((3 == 3) or (3 == 2)) return True
3==3 is true,3 == 2 false
print((4 == 3) or (3 == 2)) return False
4 == 3 is false , 3 == 2 is false
not : not operator returns the opposite value, if expression evaluated True not operator returns false vice versa
print(not(2 == 3)) returns True True
print(not(3 == 3)) returns False
bool: bool operator return either True or False
For following bool operator always return false:
None,False, 0, 0.0,{},(),[], empty string, empty list, empty tuple, empty dictionary etc
print(bool(False))
print(bool(None))
print(bool(0))
above result is False
For following bool operator always return true:
True, 1, 2, string should not empty , list with elements, tuple with elements, dictionary with elements etc
print(bool(True))
print(bool(1))
above result is True
List datatype in python:
-List datatype used to store multiple types of data into one variable.
-you can add , delete items if you want .
-List allow duplicates
-List items accessed based on index , index starts with 0
list1 = ["C", "C++","Python",15,2.15] #ceating a list
List functions:
len( ) : return the length of the list function that is number of elements in a list
len(list1) result is 5
list1[0] : return the first element in the list
print(list1[0]) result is CC
list1[0] : return ".NET" : replacing or updating first element in the list
print(list1) : ['.NET', 'C++', 'Python', 15, 2.15]
append(): Append the element to the list in last position.
list1.append("Java") result is ['C', 'C++', 'Python', 15, 2.15, 'Java']
del : deleting the list item based on give index
list1 = ["C", "C++","Python",15,2.15]
del list1[4] result is ['C', 'C++', 'Python', 15]
list1 = ["C", "C++","Python",15,2.15] #list declaration
pop( ) : removing an element from the list based on index
list1.pop(0) return result ['C++', 'Python', 15, 2.15]
remove( ): removing an element from the list based on value not based on index, if list having duplicates only first occurrence only removed
list1 = ["C++","Python",15,2.15,"C","C"]
list1.remove("C") result is ['C++', 'Python', 15, 2.15, 'C']
insert( ) : Inserting an element into the specified index.
list1 = ["C++","Python",15,2.15,"C"]
list1.insert(5,".NET") result is ['C++', 'Python', 15, 2.15, 'C', '.NET']
index() : return an index of specified element from the list
list1 = ["C++","Python",15,2.15,"C"]
print(list1.index(2.15)) result is 3
count(): return the no of times element occurrences
list1 = ["C++","Python",15,2.15,"C",15]
print(list1.count(15)) result is 2
sort() : return the sorted elements list in ascending order , if your applying sort function on list list should contain one type of data otherwise throwing the exception type error.
sorted(list2) : return the elements of a list in ascending order and creates a new list at the same time.
sorted(list2, reverse = True): return the elements of a list in descending order and creates a new list at the same time
list2= ["C++","Python",15,2.15,"C",15]
print(list2.sort()) return TypeError: '<' not supported between instances of 'int' and 'str'
list2= [11,9,10]
list2.sort()
print(list2) result is [9, 10, 11]
list2= ["1","2","0"]
list2.sort()
print(list2) result is ['0', '1', '2']
reverse() : return the elements in reverse order.
list2= [11,9,10]
list2.reverse()
print(list2) resulrt is [10, 9, 11]
Concatenation of two list using + operator
list1 = ["C++","Python",15,2.15,"C",15]
list2= [11,9,10,7]
print(list1+list2) result is ['C++', 'Python', 15, 2.15, 'C', 15, 11, 9, 10, 7]
If want repeat the list n no of times use * operator
list2= [11,9,10,7]
print(list2 * 3) return [11, 9, 10, 7, 11, 9, 10, 7, 11, 9, 10, 7]
min() return minimum element from the list
list2= [11,9,10,7]
print(min(list2)) result is 7
max() return the maximum element from the list from the list
list2= [11,9,10,7]
print(max(list2)) result is 11
extend() is used to append the list to another list
list1 = ["A","B","C"]
list2= [11,9,10,7]
list1.extend(list2)
print(list1) result is ['A', 'B', 'C', 11, 9, 10, 7]
Lists - slicing operations:
Slicing mainly mend for getting desired list output from existing list
slic_list1 =["C++","Python",15,2.15,"C","Java","SQL",".NET"]
print(slic_list1[3:]) returns the result [2.15, 'C', 'Java', 'SQL', '.NET']
print(slic_list1[1:5]) returns the result ['Python', 15, 2.15, 'C']
print(slic_list1[:10]) returns the result ['C++', 'Python', 15, 2.15, 'C', 'Java', 'SQL', '.NET']
print(slic_list1[:]) returns the result ['C++', 'Python', 15, 2.15, 'C', 'Java', 'SQL', '.NET']
print(slic_list1[-1]) returns the result .NET.NET
print(slic_list1[-2]) returns the result SQLSQL
print(slic_list1[::2]) returns the result ['C++', 15, 'C', 'SQL']
print(slic_list1[::-1]) returns the result ['.NET', 'SQL', 'Java', 'C', 2.15, 15, 'Python', 'C++']
Sets in Python:
-Sets datatype used to store multiple types of data into one variable.
-Sets not allowing duplicates.
-Like List you can add delete elements from Sets.
-Sets are unordered and unindexed
my_set1 = {"C", 34, True, 40, "Phyton"}
Crating set from list and tuple:
my_list1 = [101, 102, 103, 104, 105, 105, 105, 101]
my_set1 = set(my_list1) # removing the duplicates from the list returning output below
print(my_set1)
output: {101, 102, 103, 104, 105}
my_string1 = "xxxyyyzzzaaa"
my_set1 = set(my_string1) #removing the duplicates from the string returning output below
print(my_set1)
output : {'y', 'z', 'x', 'a'}
if observe the above from list and string set created as i said above duplicates removed.
Sets functions:
my_set1 = {101, 102, 103, 104, 105}
len( ): is return the length of the set means number of elements in set
print(len(my_set1 )) result is 5
add( ): used to add an element to set.
my_set1.add(106)
print(my_set1 ) result is {101, 102, 103, 104, 105, 106}
remove( ) : used to remove an element from set.
my_set1.remove(106)
print(my_set1) result is {101, 102, 103, 104, 105}
my_set1 = {"C","C++","Python"}
my_set2={"HTML" , "JavaScript","Jquery","C"}
intersection() : This method will return common elements from both the sets
my_set1 = my_set1.intersection(my_set2)
print(my_set1) return {'C'}
difference() : Returns the elements that first set has and second set doesn't
my_set1 = my_set1.difference(my_set2)
print(my_set1) return output {'C++', 'Python'}
union() :
-returned elements from both the sets.
-duplicates not allowed
my_set1 = my_set1.union(my_set2)
print(my_set1) return output {'C++', 'Javascript', 'C', 'Jquery', 'HTML', 'Python'}
pop() : Removes a random element from the set because set not having index concept.
my_set1 = my_set1.pop()
print(my_set1) return output C
clear(): Clear the set and result is empty set.
my_set1 = my_set1.clear()
print(my_set1) return output None
Frozensets:
-Frozensets are immutable , we can not delete , add an elements to a set
-Frozenset always contain an elements which are there at the time of declaration
Declaration of Frozenste
fs1 = frozenset(my_list1)
type(fs1)
<class 'frozenset'> #the result
fs1.add(10)
AttributeError: 'frozenset' object has no attribute 'add'
fs1.remove(1)
AttributeError: 'frozenset' object has no attribute 'remove'
fs1.pop()
AttributeError: 'frozenset' object has no attribute 'pop'
fs1.clear()
AttributeError: 'frozenset' object has no attribute 'clear'
#As we discussed frozensets are immutable so that we are getting error , if you are trying change anything in set.
What is a strip( ) function?
This function is used to removed space before start and end of the string.
t=" Welcome "
t.strip() returns "welcome"
For Part2 Click here
No comments:
Post a Comment