Python Chart
Python Chart
Example:
1. print ('Hello Python')
Output:
Hello World
Python program to do arithmetical
operations
Example -
1. # Store input numbers:
2. num1 = input('Enter first number: ')
3. num2 = input('Enter second number: ')
4.
5. # Add two numbers
6. sum = float(num1) + float(num2)
7. # Subtract two numbers
8. min = float(num1) - float(num2)
9. # Multiply two numbers
10. mul = float(num1) * float(num2)
11. #Divide two numbers
12. div = float(num1) / float(num2)
13. # Display the sum
14. print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
15.
16. # Display the subtraction
17. print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))
18. # Display the multiplication
19. print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
20. # Display the division
21. print('The division of {0} and {1} is {2}'.format(num1, num2, div))
Output:
1. if expression:
2. statement
Example 1
1. num = int(input("enter the number?"))
2. if num%2 == 0:
3. print("Number is even")
Output:
1. for iterating_var in sequence:
2. statement(s)
1. str = "Python"
2. for i in str:
3. print(i)
Output:
P
y
t
h
o
n
Python Dictionary
Python Dictionary is used to store the data in a key-value pair format. The dictionary
is the data type in Python, which can simulate the real-life data arrangement where
some specific value exists for some particular key. It is the mutable data-structure.
The dictionary is defined into element Keys and values.
1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
2. print(type(Employee))
3. print("printing Employee data .... ")
4. print(Employee)
Output
<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Python NumPy
NumPy stands for numeric python which is a python package for the computation
and processing of the multidimensional and single dimensional array elements.
With the revolution of data science, data analysis libraries like NumPy, SciPy, Pandas,
etc. have seen a lot of growth. With a much easier syntax than other programming
languages, python is the first choice language for the data scientist.
NumPy provides a convenient and efficient way to handle the vast amount of data.
NumPy is also very convenient with Matrix multiplication and data reshaping. NumPy
is fast which makes it reasonable to work with a large set of data.
There are the following advantages of using NumPy for data analysis.
PROGRAM:
1. #Represents the node of the list.
2. class Node:
3. def __init__(self,data):
4. self.data = data;
5. self.next = None;
6.
7. class CreateList:
8. #Declaring head and tail pointer as null.
9. def __init__(self):
10. self.count = 0;
11. self.head = Node(None);
12. self.tail = Node(None);
13. self.head.next = self.tail;
14. self.tail.next = self.head;
15.
16. #This function will add the new node at the end of the list.
17. def add(self,data):
18. newNode = Node(data);
19. #Checks if the list is empty.
20. if self.head.data is None:
21. #If list is empty, both head and tail would point to new node.
22. self.head = newNode;
23. self.tail = newNode;
24. newNode.next = self.head;
25. else:
26. #tail will point to new node.
27. self.tail.next = newNode;
28. #New node will become new tail.
29. self.tail = newNode;
30. #Since, it is circular linked list tail will point to head.
31. self.tail.next = self.head;
32.
33. #This function will count the nodes of circular linked list
34. def countNodes(self):
35. current = self.head;
36. self.count=self.count+1;
37. while(current.next != self.head):
38. self.count=self.count+1;
39. current = current.next;
40. print("Count of nodes present in circular linked list: "),
41. print(self.count);
42.
43.
44. class CircularLinkedList:
45. cl = CreateList();
46. #Adds data to the list
47. cl.add(1);
48. cl.add(2);
49. cl.add(4);
50. cl.add(1);
51. cl.add(2);
52. cl.add(3);
53. #Displays all the nodes present in the list
54. cl.countNodes();
Output:
1. T1 = (101, "Peter", 22)
2. T2 = ("Apple", "Banana", "Orange")
3. T3 = 10,20,30,40,50
4.
5. print(type(T1))
6. print(type(T2))
7. print(type(T3))
Output:
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
Python Set
A Python set is the collection of the unordered items. Each element in the set must
be unique, immutable, and the sets remove the duplicate elements. Sets are mutable
which means we can modify it after its creation.
1. Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"}
2. print(Days)
3. print(type(Days))
4. print("looping through the set elements ... ")
5. for i in Days:
6. print(i)
Output: