-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInfo.py
More file actions
189 lines (139 loc) · 5.22 KB
/
PythonInfo.py
File metadata and controls
189 lines (139 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# To run the file, simply call "python [Filename].py" by using the "cmd Terminal".
# -------------------------------------------------------------------------------------------------------------------------------
# Importing
import math
math.sqrt(9) # Calculate the square root of 9
import time
# Print the current time
print("The current time is: ", time.ctime())
# -------------------------------------------------------------------------------------------------------------------------------
# How to Comment
a = 5
# a = a + 1
print(a)
# -------------------------------------------------------------------------------------------------------------------------------
# Indenting
x = 1
if 2*x == 1:
print("yes")
# -------------------------------------------------------------------------------------------------------------------------------
# Variables
x = 1
x = x + 2
# Print the value of x
print("The value of x is: ", x)
# -------------------------------------------------------------------------------------------------------------------------------
# Strings
x = "abc"
y = "de"
z = x + y
# Print the value of z
print("The value of z is: ", z)
# Print the type of z
print("The type of z is: ", type(z))
z = z[1:3] # Slicing the string | Similar to arrays
# Print the sliced value of z
print("The sliced value of z is: ", z)
# -------------------------------------------------------------------------------------------------------------------------------
# Functions
def f(x):
y = x + 2
return y
# Call the function f with argument 5
f(5)
# Print the result of the function call
print("This is a function call with argument 5: ", f(5))
# Multiple functions
def f(x):
return 2*x
def g(y):
return 1 + y
# Call the functions f and g with argument 4
g(f(4))
f(g(4))
# Print the result of the function calls
print("This is a function call with argument 4: ", g(f(4))) # (2*4)+1
print("This is a function call with argument 4: ", f(g(4))) # 2*(1+4)
# -------------------------------------------------------------------------------------------------------------------------------
# if Statements
x = 2
if x+1 == 3:
print(x)
if x+1 == 4:
print(x)
# if-else Statement
x = 2
if x == 1:
print(5)
else:
print(1)
# -------------------------------------------------------------------------------------------------------------------------------
# Loops
for i in range(1, 100):
if i%2 == 0: # Check if i is even
print("This is a loop with i = ", i)
# -------------------------------------------------------------------------------------------------------------------------------
# Arrays
a = [1, 2, 3, 4]
print("The array a is: ", a)
a.append(8) # Append 8 to the array a
print("The array a after appending 8 is: ", a)
# Looping through arrays in 2 ways
for i in range(len(a)):
print("This is a loop with a[i] = ", a[i])
for x in a:
print("This is a loop with x = ", x)
# Delete from array
a = [1, 2, 3, 4, 5]
a.pop() # Remove the last element from the array
print("The array a after popping the last element is: ", a)
# -------------------------------------------------------------------------------------------------------------------------------
# Dictionary
d = {'cat': 'meow', 'dog': 'bark', 'bird': 'chirp'}
# Print the value associated with the key 'cat'
print("The value associated with the key 'cat' is: ", d['cat'])
d['dog'] = 'run' # Assign a new value to the key 'dog'
# Print everything in the dictionary
print("Dictionary:", d)
# -------------------------------------------------------------------------------------------------------------------------------
# Sets
# 1
# Set vs Dictionary adding elements
s = set() # Create an empty set
d = {} # Create an empty dictionary
s.add(3) # Add an element to the set
s.add(6)
s.add(9)
s.remove(9) # Remove an element from the set
d[3] = 45 # Add a key-value pair to the dictionary
d[6] = 89
d[1] = 89
d[1] = 67 # Update the value for the key 1 in the dictionary
print("Set:", s)
print("Dictionary:", d)
# Sets cannot have duplicates
s.add(1)
s.add(1) # Adding a duplicate element to the set
print("Set after adding duplicates:", s) # The set will still only contain unique elements
# 2
# Dictionary & Set that associates the elements to something
s = set(['cat', 'dog', 'bird']) # Create a set with initial elements
d = {'cat':'joe', 'dog':'janet', 'bird':'jack'} # Create a dictionary with initial key-value pairs
print("Set with initial elements:", s)
print("Dictionary with initial key-value pairs:", d)
# 3
# Set operations
s1 = set([1, 2, 3])
s2 = set([2, 3, 4])
# Intersection
s3 = s1.intersection(s2) # Find common elements in both Sets
print("Intersection of s1 and s2:", s3) # Print the intersection of the two Sets
# Union
s3 = s1.union(s2) # Combine all unique elements from both Sets
print("Union of s1 and s2:", s3) # Print the union of the two Sets
# Difference
s3 = s1.difference(s2) # Find elements in s1 that are not in s2
print("Difference of s1 and s2 (s1 - s2):", s3) # Print the difference of the two Sets
len(s3) # Get the number of elements in the Set
len(d) # Get the number of key-value pairs in the Dictionary
# -------------------------------------------------------------------------------------------------------------------------------