Tuesday, October 22, 2019

Learning python Day 7


Day7
Functions
Two types of function: library and user defined functions
To define function we use def
def disp():
   
print("Hello")

print("Started")
disp()  
#function calling
print("Back")
run the function CTL+shift+fn+F10
Started
Hello
Back
def findsum(x,y):
   
print("Sum=",x+y)


a=
int(input("Enter a number "))   #5
b=int(input("Enter 2nd number "))   #7
findsum(a,b)

findsum(
10,20)
findsum(
100,20)
run the function CTL+shift+fn+F10
Enter a number 7
Enter 2nd number 5
Sum= 12
Sum= 30
Sum= 120
def findsum(x,y):
   
return x+y

a=
int(input("Enter a number "))   #5
b=int(input("Enter 2nd number "))   #7
c=findsum(a,b)
print("Sum=",c)
run the function CTL+shift+fn+F10
Enter a number 7
Enter 2nd number 5
Sum= 12
def disp(n):
   
if(n%2==0):
       
print(n,"is even")
   
else:
       
print(n,"is odd")

a=
int(input("Enter a number"))
disp(a)
disp(
34)
disp(
98)
run the function CTL+shift+fn+F10
Enter a number7
7 is odd
34 is even
98 is even
def findlargest(a,b,c):
   
if(a>b and a>c):
       
print(a,'is largest')
   
elif(b>a and b>c):
       
print(b,'is largest')
   
elif(c>a and c>b):
       
print(c,'is largest')
   
else:
       
print("Can't find the largest one")


findlargest(
12,54,23)
run the function CTL+shift+fn+F10
54 is largest
def table(n):
   
print("Table of",n,"is")
   
for i in range(1,11):
       
print(n*i)

a=
int(input("Enter a number"))
table(a)
table(
9)
run the function CTL+shift+fn+F10

Enter a number7
Table of 7 is
7
14
21
28
35
42
49
56
63
70
Table of 9 is
9
18
27
36
45
54
63
72
81
90


Learning Python - Day 8

Monday, October 21, 2019

Learning Python- Day 6


DAY 6

dict1={
   
"Abhi":"Noida",
   
"Nitin":"Gr. Noida"
    
}
print("Dict is",dict1)
run the function CTL+shift+fn+F10
Dict is {'Abhi': 'Noida', 'Nitin': 'Gr. Noida'}

In the above program, Abhi is the key and Noida is the value. Key and value will always be together in dictionary. The key is analogous to the index, they are like addresses but they don’t have to be
integers. They are usually characters; the values are
similar to the elements in a list and contain information.
To create a dictionary, we use curly brackets. The keys are the first elements; they must
be immutable and unique. Each key is followed by a value separated
by a colon. The values can be immutable, mutable and duplicates.
Each key and value pair is separated by a comma.

To add new value to the dictionary

dict1[
"Rohit"]="Delhi"
dict1["Tom"]="Pune"
dict1["John"]="Agra"
dict1["Joy"]="Jaipur"

Program:
dict1={
   
"Abhi":"Noida",
   
"Nitin":"Gr. Noida"
    
}
dict1[
"Rohit"]="Delhi"
dict1["Tom"]="Pune"
dict1["John"]="Agra"
dict1["Joy"]="Jaipur"
print(dict1)
run the function CTL+shift+fn+F10
{'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Rohit': 'Delhi', 'Tom': 'Pune', 'John': 'Agra', 'Joy': 'Jaipur'}



dict2=dict1.copy()
print("Dict2 is ",dict2)
run the function CTL+shift+fn+F10
Dict2 is  {'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Rohit': 'Delhi', 'Tom': 'Pune', 'John': 'Agra', 'Joy': 'Jaipur'}

print(dict2["Tom"])
run the function CTL+shift+fn+F10
Pune

print(dict2.keys())
run the function CTL+shift+fn+F10

dict_keys(['Abhi', 'Nitin', 'Rohit', 'Tom', 'John', 'Joy'])

print(dict2.values())
run the function CTL+shift+fn+F10
dict_values(['Noida', 'Gr. Noida', 'Delhi', 'Pune', 'Agra', 'Jaipur'])

print(dict2.items())
run the function CTL+shift+fn+F10
dict_items([('Abhi', 'Noida'), ('Nitin', 'Gr. Noida'), ('Rohit', 'Delhi'), ('Tom', 'Pune'), ('John', 'Agra'),

print(dict2.get("Nitin"))
run the function CTL+shift+fn+F10
Gr. Noida

print(dict2.popitem())
run the function CTL+shift+fn+F10
('Joy', 'Jaipur')

print(dict2.pop("Rohit"))
run the function CTL+shift+fn+F10
Delhi

print(dict2)
run the function CTL+shift+fn+F10

{'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Tom': 'Pune', 'John': 'Agra'}

print("Nitin" in dict2)
print("Joy" in dict2)
print("Joy" not in dict2)

dict2.clear()
print(dict2)

run the function CTL+shift+fn+F10

{}

del dict2
#print(dict2)
run the function CTL+shift+fn+F10

   print(dict2)
NameError: name 'dict2' is not defined



for i in dict1.items():
    print(i)
run the function CTL+shift+fn+F10

('Abhi', 'Noida')
('Nitin', 'Gr. Noida')
('Rohit', 'Delhi')
('Tom', 'Pune')
('John', 'Agra')
('Joy', 'Jaipur')

for i in dict1.keys():
    print(i)
run the function CTL+shift+fn+F10


Abhi
Nitin
Rohit
Tom
John
Joy


for i in dict1.values():
    print(i)
run the function CTL+shift+fn+F10
Noida
Gr. Noida
Delhi
Pune
Agra
Jaipur

for i,j in dict1.items():
    print(i,j)
run the function CTL+shift+fn+F10
Abhi Noida
Nitin Gr. Noida
Rohit Delhi
Tom Pune
John Agra
Joy Jaipur

SETS


Let’s cover sets; they are also a type of collection.Sets are a type of collection. This means that like lists and tuples, you can input different python types. Unlike lists and tuples they are unordered.This means sets do not record element position. Sets only have unique elements. This means there is only one of a particular element in a set.To define a set, you use curly brackets You place the elements of a set within the curly brackets. You notice there are duplicate items. When the actual set is created, duplicate items will not be present.
You can convert a list to a set by using the function set; this is called type-casting. You simply use the list as the input to the function set. The result will be a list converted to a set.

s1={10,20,30,40,50,10}
print(s1)
run the function CTL+shift+fn+F10
{40, 10, 50, 20, 30}

s1.add(
60)
run the function CTL+shift+fn+F10
{40, 10, 50, 20, 60, 30}

s1.update([
70,80,90])
run the function CTL+shift+fn+F10

{70, 40, 10, 80, 50, 20, 90, 30}


print(len(s1))
run the function CTL+shift+fn+F10
8
print(max(s1))
run the function CTL+shift+fn+F10
90
print(min(s1))
run the function CTL+shift+fn+F10
10

s1.remove(
10)
print(s1)
run the function CTL+shift+fn+F10
{70, 40, 80, 50, 20, 90, 30}

#s1.remove(100)
s1.discard(1000)

print(s1.pop())

print(100 in s1)
print(100 not in s1)

s2={
10,20,30,40,50}
s3={
40,50,60,70,80}

s4=s2.union(s3)
print(s4)

s5=s2.intersection(s3)
print(s5)

s6=s2.difference(s3)
print(s6)

s7=s2.symmetric_difference(s3)
print(s7)

print(s2,s3)

#s2.intersection_update(s3)
#s2.difference_update(s3)
s2.symmetric_difference_update(s3)
print(s2)


s8={
10,20,30,40}
s9={
30,40}
s10={
50,60,70}

print(s9.issubset(s8))
print(s8.issuperset(s9))
print(s8.isdisjoint(s10))

Learning Python- Day 7

Sunday, October 20, 2019

Learning Python- Day 5


Day 5

Lists are also a popular data structure in Python.
Lists are also an ordered sequence.
Here is a list L. A list is represented with square brackets.
In many respects lists are like tuples, one key difference is they are mutable.
Lists can contain strings, floats, integers.
We can nest other lists.
We also nest tuples and other data structures; the same indexing conventions apply for nesting.

Like tuples, each element of a list can be accessed via an index.

list1=[10,20,30,40,50,60]
print("list is",list1)

run the function CTL+shift+fn+F10
list is [10, 20, 30, 40, 50, 60]
list1=[10,20,30,40,50,60]
print("Length of list is",len(list1))
run the function CTL+shift+fn+F10
Length of list is 6
list1=[10,20,30,40,50,60]
list1.append(
70)
print(list1)
run the function CTL+shift+fn+F10
[10, 20, 30, 40, 50, 60, 70]

To add multiple entries to the list
list1=[10,20,30,40,50,60]
list1.extend([
80,90,100])
print(list1)
run the function CTL+shift+fn+F10
[10, 20, 30, 40, 50, 60, 80, 90, 100]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2)
run the function CTL+shift+fn+F10
[10, 20, 30, 40, 50, 60]
To remove last element from the list
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.pop())
run the function CTL+shift+fn+F10
60
list1=[10,20,30,40,50,60]
list3=list1
print("List3 is ",list3)
print(list3.pop())
print(list3)
run the function CTL+shift+fn+F10
List3 is  [10, 20, 30, 40, 50, 60]
60
[10, 20, 30, 40, 50]
Any change in one list will automatically make change in other list when you use = instead of copy
list1=[10,20,30,40,50,60]
list2=list1.copy()
print("List2 is",list2)
print(list2[2])
run the function CTL+shift+fn+F10
List2 is [10, 20, 30, 40, 50, 60]
30
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[2:7])
run the function CTL+shift+fn+F10
[30, 40, 50, 60]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[2:7:2])
run the function CTL+shift+fn+F10
[30, 50]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[::-1])
run the function CTL+shift+fn+F10
[60, 50, 40, 30, 20, 10]
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2[-2])
run the function CTL+shift+fn+F10
50
To insert a particular element at a particular index
list1=[10,20,30,40,50,60]
list2=list1.copy()
list2.insert(
2,110)
print(list2)
run the function CTL+shift+fn+F10
[10, 20, 110, 30, 40, 50, 60]
To remove particular element
list1=[10,20,30,40,50,60]
list2=list1.copy()
list2.insert(
2,110)
print(list2)
list2.remove(
110)
print("list2 is ",list2)
run the function CTL+shift+fn+F10
[10, 20, 110, 30, 40, 50, 60]
list2 is  [10, 20, 30, 40, 50, 60]

Removing any element from specific index
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.pop(3))
run the function CTL+shift+fn+F10
40
To find index of any element
list1=[10,20,30,40,50,60]
list2=list1.copy()
print(list2.index(50))
run the function CTL+shift+fn+F10
4
To count no of times element is repeating
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
print(list4.count(2))
run the function CTL+shift+fn+F10
7
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
list4.sort()
print(list4)
run the function CTL+shift+fn+F10
[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]
list4=[1,2,3,2,1,2,1,2,3,2,1,2,3,2]
list4.reverse()
print(list4)
run the function CTL+shift+fn+F10
[2, 3, 2, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 1]
list5=[2,7,4,3,5]
print(max(list5))
run the function CTL+shift+fn+F10
7
print(min(list5))
print(sum(list5))

list6=[i for i in range(10)]
print(list6)
run the function CTL+shift+fn+F10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list7=[i for i in range(2,8)]
print(list7)
run the function CTL+shift+fn+F10
[2, 3, 4, 5, 6, 7]
Converting string to list
str1="hello This is Python string"
list1=str1.split(" ")
print(list1)
run the function CTL+shift+fn+F10
['hello', 'This', 'is', 'Python', 'string']

Converting list to string
str1="hello This is Python string"
list1=str1.split(" ")
print(list1)
str2=
" ".join(list1)
print(str2)
run the function CTL+shift+fn+F10
['hello', 'This', 'is', 'Python', 'string']
hello This is Python string
str1=input("Enter a string")
print("String is ",str1)
list1=str1.split(
" ")
print("List1 is ",list1)
list1.reverse()

print("List1 is ",list1)
str2=
" ".join(list1)
print(str2)
run the function CTL+shift+fn+F10
Enter a stringthis is a sting of india
String is  this is a sting of india
List1 is  ['this', 'is', 'a', 'sting', 'of', 'india']
List1 is  ['india', 'of', 'sting', 'a', 'is', 'this']
india of sting a is this

Tuples
tuple1=(10,20,30,40,50)
print(len(tuple1))
print(tuple1[2])
print(tuple1[1:4])
print(tuple1[1:4:2])
print(tuple1[::-1])
print(tuple1.index(40))
print(tuple1.count(30))
print(min(tuple1))
print(max(tuple1))
print(sum(tuple1))

t1=(
100,)
print(t1,type(t1))
run the function CTL+shift+fn+F10
5
30
(20, 30, 40)
(20, 40)
(50, 40, 30, 20, 10)
3
1
10
50
150
(100,) <class 'tuple'>
Learning Python Day 6

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...