print("با درود و آرزوی سلامتی")
با درود و آرزوی سلامتی
print(2)
2
print(2+4)
6
4 + 5
9
5 * 4
20
8 / 2
4.0
(2 + 4 * (1 + 5))*2
52
2 ** 3
8
5 / 2
2.5
5//2
2
7/4, 7//4
(1.75, 1)
7 % 4
3
x = 5
x
5
5 * x
25
x = 2
x
2
y = 3
y
3
z = x + y
z
5
w = z ** 2
x, y ,z, w
(2, 3, 5, 25)
9 ** 0.5
3.0
25 ** 1/2
12.5
25 ** (1/2)
5.0
r = 2.25
r
2.25
abs(-34)
34
complex(7, 3)
(7+3j)
C1 = 2 + 5j
C1
(2+5j)
C2 = 3 + 4j
C2
(3+4j)
C3 = C1 + C2
C3
(5+9j)
C4 = C1 * C2
C4
(-14+23j)
b = book
b
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/2291144819.py in <module> ----> 1 b = book 2 b NameError: name 'book' is not defined
b = "book"
b
'book'
c = 'car'
c
'car'
type(x)
int
type(r)
float
type(C1)
complex
type(b)
str
type(c)
str
B = (4 == 2 * 2)
B
True
B1 = (4 > 7)
B1
False
type(B)
bool
type(B1)
bool
t = 1.234567
print(f"Default output gives t = {t}.")
print(f"We can set the precision: t = {t:.2}.")
print(f"Or control the number of decimals: t = {t:.2f}.")
print(f"We may set the space used for the output: t = {t:10.2f}.")
Default output gives t = 1.234567. We can set the precision: t = 1.2. Or control the number of decimals: t = 1.23. We may set the space used for the output: t = 1.23.
r = 587
print(f"Integer set to occupy exactly 8 chars of space: r = {r:9d}")
Integer set to occupy exactly 8 chars of space: r = 587
a = 786345687.12
b = 1200555.345
print(f"Without the format specifier: a = {a}, b = {b}.")
print(f"With the format specifier: a = {a:g}, b = {b:g}.")
Without the format specifier: a = 786345687.12, b = 1200555.345. With the format specifier: a = 7.86346e+08, b = 1.20056e+06.
print(f"{2*3}")
6
print("{2*3}")
{2*3}
print(f"{2*3:f}")
6.000000
print(f"{2*3:.2f}")
6.00
print(f"{2*3:.5f}")
6.00000
print(f"{2*3:20.5f}")
6.00000
print(f"{2*3:.2d}")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_31549/1600932916.py in <module> ----> 1 print(f"{2*3:.2d}") ValueError: Precision not allowed in integer format specifier
print(f"{2*3:d}")
6
print(f"{2*3:20d}")
6
print(f"{2*3:g}")
6
print(f"{2*3:20g}")
6
print(f"{2*3:20.2g}")
6
print(f"{2*30000:20g}")
60000
print(f"{2*3000000:20g}")
6e+06
print(f"{2*300000:20g}")
600000
print(f"{2*30000000:20g}")
6e+07
b1 = "b{}k".format("")
print(b1)
bk
b1 = "b{}k".format("oo")
print(b1)
book
p = 1500.258
print(p)
1500.258
p1 = "The price of the paper is {:.0f}".format(p)
print(p1)
The price of the paper is 1500
p2 = "The price of the paper is {:.1f}".format(p)
print(p2)
The price of the paper is 1500.3
p3 = "The price of the paper is {:.2f}".format(p)
print(p3)
The price of the paper is 1500.26
p4 = "The price of the paper is {:.3f}".format(p)
print(p4)
The price of the paper is 1500.258
print("{:.2f}".format(2*3))
6.00
print("{:10.3f}".format(2*3))
6.000
print("{:10.3g}".format(2*3))
6
print("{:10.3d}".format(2*3))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_31549/3815388816.py in <module> ----> 1 print("{:10.3d}".format(2*3)) ValueError: Precision not allowed in integer format specifier
print("{:10d}".format(2*3))
6
print("{:10.3g}".format(2*300000))
6e+05
print("2+5 = {2+5}")
2+5 = {2+5}
print(f"2+5 = {2+5}")
2+5 = 7
print(f"2+5 \n = {2+5}")
2+5 = 7
hello = "Hello, World!"
print(hello + hello)
Hello, World!Hello, World!
hello = "Hello, World! "
print(hello + hello)
Hello, World! Hello, World!
print(hello * 5)
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!
x1 = 4
x2 = "4"
print (x1 + x1)
print (x2 + x2)
8 44
print(x1 + x2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/3942653987.py in <module> ----> 1 print(x1 + x2) TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(x1 + int(x2))
8
print(str(x1) + x2)
44
print(x1 + float(x2))
8.0
print(x1, type(x1), type(str(x1)), type(float(x1)), type(complex(x1)), type(bool(x1)))
4 <class 'int'> <class 'str'> <class 'float'> <class 'complex'> <class 'bool'>
x = range(5, 20, 4) # Start, Stop, Step
for n in x:
print(n)
5 9 13 17
x = range(20, 2, -4) # Start, Stop, Negative Step
for n in x:
print(n)
20 16 12 8
x = range(2, 6) # By default Step =1
for n in x:
print(n)
2 3 4 5
x = range(5) # By default Start = 0, Step =1
for n in x:
print(n)
0 1 2 3 4
print(list(range(0))) # empty range
print(list(range(10))) # using range(stop)
print(list(range(1, 10))) # using range(start, stop)
print(list(range(1, 10, 2))) # using range(start, stop, step)
[] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 3, 5, 7, 9]
start = 2
stop = 20
step = 3
print(list(range(start, stop, step)))
[2, 5, 8, 11, 14, 17]
start = 2
stop = -16
step = -3
print(list(range(start, stop, step)))
stop = 16
# value constraint not met
print(list(range(start, stop, step)))
[2, -1, -4, -7, -10, -13] []
x = [1, 2, 5, 8, 15]
x
[1, 2, 5, 8, 15]
x = [1, 2, 5, 8, 15, "pencil"]
x
[1, 2, 5, 8, 15, 'pencil']
x = [1, 2, 5, 8, 15, "pencil", [4, 8]]
x
[1, 2, 5, 8, 15, 'pencil', [4, 8]]
x[0]
1
x[1]
2
x[2]
5
x[3]
8
x[4]
15
x[5]
'pencil'
x[6]
[4, 8]
x[6][0]
4
x[6][1]
8
print(x[1])
x[1] + 4
2
6
y1 = [1, 2, 5, 8, 15, "pencil", [4, 8]]
y1
[1, 2, 5, 8, 15, 'pencil', [4, 8]]
y1[4:]
[15, 'pencil', [4, 8], 50]
y1[3:6]
[8, 15, 'pencil']
y1.append(50)
y1
[1, 2, 5, 8, 15, 'pencil', [4, 8], 50]
y2 = y1 + [60, 61]
y2
[1, 2, 5, 8, 15, 'pencil', [4, 8], 50, 60, 61]
y3 = y2
del(y3[6])
y3
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61]
2 in y2 # is the value 9 found in n? True/False
True
[4, 8] in y2
False
[4, 8] in y3
False
print(y1)
len(y1)
[1, 2, 5, 8, 15, 'pencil', [4, 8], 50]
8
print(y2)
len(y2)
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61]
9
print(y3)
len(y3)
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61]
9
y4 = y3 + [60, 60, 60]
print(y4)
y4.count(60) # Return the number of times x appears in the list.
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61, 60, 60, 60]
4
y3.count("pencil")
1
my_combined_list = ['alpha', 8]+['beta', False, 9] # Combining two lists -- Method 1
print(mylist)
['alpha', 8, 'beta', False, 9]
mylist = ['alpha', 8]
mylist.extend(['beta', False, 9]) # Combining two lists -- Method 2
print(mylist)
['alpha', 8, 'beta', False, 9]
y5 = y3
y5.append(15)
print(y5)
y5.count(15)
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61, 15]
2
list1 = list(range(10))
print(list1)
list1.append(11) # Adding an element to a list
print(list1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
list2 = list(range(15))
print(list2)
list2.insert(5,'Book') # inserting an element to a list
print(list2)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [0, 1, 2, 3, 4, 'Book', 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
list3 = list(range(15))
print(list3)
list3.pop(8) # removing an element from a list
print(list3)
list3.pop() # removing the last element from a list
print(list3)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14] [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13]
y5.index(8)
3
y6 = y5
print(y6)
y6.reverse()
print(y6)
[1, 2, 5, 8, 15, 'pencil', 50, 60, 61, 15] [15, 61, 60, 50, 'pencil', 15, 8, 5, 2, 1]
a = [1,2,3,4]
b = a
b[1] = 6
a
[1, 6, 3, 4]
x = [1, 2, 3, 4]
x
[1, 2, 3, 4]
x[1] = 14 # list is not a constant and can be changed
x
[1, 14, 3, 4]
# sorting a list
x = [6, 4, 8, 3, 12]
x.sort()
print(x)
[3, 4, 6, 8, 12]
sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
x = ['abc', 'ab', 'abcdefgh', 'abcde']
x.sort(key = len)
print(x)
['ab', 'abc', 'abcde', 'abcdefgh']
sorted("This is a test to show how Python sorted function works".split(), key=str.lower)
['a', 'function', 'how', 'is', 'Python', 'show', 'sorted', 'test', 'This', 'to', 'works']
# Slicing selectes a part of a list
x = [10, 5, 12, 4, 46, 24, 99, 0]
print(x)
y1 = x[2:6] # from index 2 to index 6
print(y1)
y2 = x[2:6:2] # from index 2 to index 6 by step 2
print(y2)
y3=x[:6] # from beginning to index 6
print(y3)
y4=x[3:] # from index 3 to end
print(y4)
y5=x[-3:] # from the third last index to the end
print(y5)
y6=x[::1] # from index 0 to the end
print(y6)
y7=x[1::2] # from index 1 to the end by step 2
print(y7)
y8=x[::-1] # reverse
print(y8)
[10, 5, 12, 4, 46, 24, 99, 0] [12, 4, 46, 24] [12, 46] [10, 5, 12, 4, 46, 24] [4, 46, 24, 99, 0] [24, 99, 0] [10, 5, 12, 4, 46, 24, 99, 0] [5, 4, 24, 0] [0, 99, 24, 46, 4, 12, 5, 10]
x = [5, 20, 8, 12, 24]
n = 0
for element in x:
print('Index '+ str(n)+ ' ===> '+ str(element))
n += 1
Index 0 ===> 5 Index 1 ===> 20 Index 2 ===> 8 Index 3 ===> 12 Index 4 ===> 24
x = [5, 20, 8, 12, 24]
for n, element in enumerate(x):
print('Index '+ str(n)+ ' ===> '+ str(element))
Index 0 ===> 5 Index 1 ===> 20 Index 2 ===> 8 Index 3 ===> 12 Index 4 ===> 24
x = (1, 2, 2, 4, 5)
x
(1, 2, 2, 4, 5)
type(x)
tuple
x[1]
2
x[-1] # We can also use negative indexing with tuples
5
x[-2]
4
x[0:3]
(1, 2, 2)
x[:]
(1, 2, 2, 4, 5)
x[0:3:2]
(1, 2)
x[::2] # Increment = 2
(1, 2, 5)
x[::-1] # Negative increament
(5, 4, 2, 2, 1)
x[1] = 25 # tuple is essentially a constant list that cannot be changed
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/2192919023.py in <module> ----> 1 x[1] = 25 # tuple is essentially a constant list that cannot be changed TypeError: 'tuple' object does not support item assignment
y = 1, 2, 3 # a tuple can be also defined without parentheses
y
(1, 2, 3)
x1 = x + (6, 7, 8) # add two tuples
x[3:]
(4, 5)
x1[3:]
(4, 5, 6, 7, 8)
x2 = (6, 4, 9), (3, 7) # nested tuple ===> tuple of tuples
x2
((6, 4, 9), (3, 7))
print(x)
print(min(x))
print(max(x))
print(x2)
print(min(x2))
print(max(x2))
(1, 2, 2, 4, 5) 1 5 ((6, 4, 9), (3, 7)) (3, 7) (6, 4, 9)
a = ('ABCDEFG')
print(max(a))
print(min(a))
G A
a = (10,9,8,7,6,5,4,3,2,1)
print(sorted(a))
print(sum(a))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 55
a = (1,2,3)
(one,two,three) = a # assigning multiple values at once
print(one)
1
a1=[1,2,3]
(one,two, three) = a1
a1
[1, 2, 3]
a2=[4,5,6]
[four,five,six] = a2
a2
[4, 5, 6]
tuple([4, 0, 6]) # converting a list to a tuple
(4, 0, 6)
tuple('Nice Book')
('N', 'i', 'c', 'e', ' ', 'B', 'o', 'o', 'k')
tuple1 = tuple()
print(tuple1)
# when an iterable(e.g., list) is passed
list1= [ 1, 2, 3, 4 ]
tuple2 = tuple(list1)
print(tuple2)
# when an iterable(e.g., string) is passed
string = "geeksforgeeks"
tuple3 = tuple(string)
print(tuple3)
()
(1, 2, 3, 4)
('g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's')
mytuple = (1,2,3,4,5)
mylist = [1,2,3,4,5]
# Append a number to the tuple
mytuple.append(6)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_23035/6683646.py in <module> 3 4 # Append a number to the tuple ----> 5 mytuple.append(6) AttributeError: 'tuple' object has no attribute 'append'
mytuple1 = (1,2,3,4,5)
mylist1 = [1,2,3,4,5]
# Append a number to the list
mylist.append(6) # adding 6 to the list
mylist.append(7) # adding 7 to the list
mylist.append(8) # adding 8 to the list
print(mylist) # removing 8 from the list
mylist.remove(8)
print(mylist)
[1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7]
tup=tuple([[1,2], 'list', {'firstletter:' 'a', 'secondletter:' 'b'}])
tup[0].append("5")
tup
([1, 2, '5'], 'list', {'firstletter:a', 'secondletter:b'})
mytuple1 = (1,2,3,4,5)
mytuple2 = (6,7,8,9,10)
mylist1 = [1,2,3,4,5]
mylist2 = [6,7,8,9,10]
print(mytuple1 + mytuple2)
print(mylist1 + mylist2)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(mytuple1 + mylist1) # we cannot concatenate a tuple to a list
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_23035/308024293.py in <module> ----> 1 print(mytuple1 + mylist1) # we cannot concatenate a tuple to a list TypeError: can only concatenate tuple (not "list") to tuple
x1 = [5, 4, 3, 2]
x1[3] = 7 # In contrast to tuples, the length of a list can vary and their contents can be modified
x1
[5, 4, 3, 7]
#Like "tuple" function for converting a list to a tuple, the "list" function converts a tuple to a list
x2 = (4, "book", 2==1+1, 8)
x3= list(x2) # converting a tuple to a list
print(x2)
print(type(x2))
print(x3)
print(type(x3))
(4, 'book', True, 8) <class 'tuple'> [4, 'book', True, 8] <class 'list'>
d1 = {1:2}
print(d1)
d1[1]
{1: 2}
2
d2 = {"necessary":"essential"}
print(d2)
d2["necessary"]
{'necessary': 'essential'}
'essential'
d3 = {"decrease":"reduce", "increase":"raise", 4:"2*2"}
print(d3)
d3["decrease"], d3["increase"], d3[4]
{'decrease': 'reduce', 'increase': 'raise', 4: '2*2'}
('reduce', 'raise', '2*2')
# when an iterable(e.g., dictionary) is passed
dict = { 1 : 'one', 2 : 'two' }
tuple3 = tuple(dict)
print(tuple3)
(1, 2)
tuple4=tuple([[3, 4,5], 'textbook', {'2:' 'two', '6:' 'six'}, 4!=3*2])# a tuple of list, string, dictionary, boolean
tuple4
([3, 4, 5], 'textbook', {'2:two', '6:six'}, True)
tuple4[1] = 'cat'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_23035/933136581.py in <module> ----> 1 tuple4[1] = 'cat' TypeError: 'tuple' object does not support item assignment
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more
import numpy as np
np
<module 'numpy' from '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/__init__.py'>
x = np.array([2, 4, 6, 8]) # here x is an array
x
array([2, 4, 6, 8])
y = [2, 4, 6, 8] # here y is a list
y
[2, 4, 6, 8]
y * 2
[2, 4, 6, 8, 2, 4, 6, 8]
x * 2
array([ 4, 8, 12, 16])
y + 2 * y
[2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8]
x + 2 * x
array([ 6, 12, 18, 24])
x.append(5) # append does not work for an array
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/1397086028.py in <module> ----> 1 x.append(5) AttributeError: 'numpy.ndarray' object has no attribute 'append'
y.append(5) # append works for an list
y
[2, 4, 6, 8, 5, 5]
y ** 2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/2162179210.py in <module> ----> 1 y ** 2 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
x ** 2
array([ 4, 16, 36, 64])
1 / y
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_89974/944063955.py in <module> ----> 1 1 / y TypeError: unsupported operand type(s) for /: 'int' and 'list'
1 / x
array([0.5 , 0.25 , 0.16666667, 0.125 ])
x = np.arange(0, 10, 1)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x = np.arange(0, 10, 2)
x
array([0, 2, 4, 6, 8])
A set is like a dictionary withouth keys.
set([5,5,2,2,2,4,9,10,10,2]) # Elements are not repeated in a set
{2, 4, 5, 9, 10}
A={6, 8, 2, 8, 10}
B={1, 3, 5, 7, 8, 2}
A.union(B) # اجتماع دو مجموعه
{1, 2, 3, 5, 6, 7, 8, 10}
A.intersection(B)
{2, 8}
import numpy as np
x = np.linspace(0, 10, 500) # (10 - 0) / (500 - 1) = 1 / 499 = 0.02004008016
x
array([ 0. , 0.02004008, 0.04008016, 0.06012024, 0.08016032,
0.1002004 , 0.12024048, 0.14028056, 0.16032064, 0.18036072,
0.2004008 , 0.22044088, 0.24048096, 0.26052104, 0.28056112,
0.3006012 , 0.32064128, 0.34068136, 0.36072144, 0.38076152,
0.4008016 , 0.42084168, 0.44088176, 0.46092184, 0.48096192,
0.501002 , 0.52104208, 0.54108216, 0.56112224, 0.58116232,
0.6012024 , 0.62124248, 0.64128257, 0.66132265, 0.68136273,
0.70140281, 0.72144289, 0.74148297, 0.76152305, 0.78156313,
0.80160321, 0.82164329, 0.84168337, 0.86172345, 0.88176353,
0.90180361, 0.92184369, 0.94188377, 0.96192385, 0.98196393,
1.00200401, 1.02204409, 1.04208417, 1.06212425, 1.08216433,
1.10220441, 1.12224449, 1.14228457, 1.16232465, 1.18236473,
1.20240481, 1.22244489, 1.24248497, 1.26252505, 1.28256513,
1.30260521, 1.32264529, 1.34268537, 1.36272545, 1.38276553,
1.40280561, 1.42284569, 1.44288577, 1.46292585, 1.48296593,
1.50300601, 1.52304609, 1.54308617, 1.56312625, 1.58316633,
1.60320641, 1.62324649, 1.64328657, 1.66332665, 1.68336673,
1.70340681, 1.72344689, 1.74348697, 1.76352705, 1.78356713,
1.80360721, 1.82364729, 1.84368737, 1.86372745, 1.88376754,
1.90380762, 1.9238477 , 1.94388778, 1.96392786, 1.98396794,
2.00400802, 2.0240481 , 2.04408818, 2.06412826, 2.08416834,
2.10420842, 2.1242485 , 2.14428858, 2.16432866, 2.18436874,
2.20440882, 2.2244489 , 2.24448898, 2.26452906, 2.28456914,
2.30460922, 2.3246493 , 2.34468938, 2.36472946, 2.38476954,
2.40480962, 2.4248497 , 2.44488978, 2.46492986, 2.48496994,
2.50501002, 2.5250501 , 2.54509018, 2.56513026, 2.58517034,
2.60521042, 2.6252505 , 2.64529058, 2.66533066, 2.68537074,
2.70541082, 2.7254509 , 2.74549098, 2.76553106, 2.78557114,
2.80561122, 2.8256513 , 2.84569138, 2.86573146, 2.88577154,
2.90581162, 2.9258517 , 2.94589178, 2.96593186, 2.98597194,
3.00601202, 3.0260521 , 3.04609218, 3.06613226, 3.08617234,
3.10621242, 3.12625251, 3.14629259, 3.16633267, 3.18637275,
3.20641283, 3.22645291, 3.24649299, 3.26653307, 3.28657315,
3.30661323, 3.32665331, 3.34669339, 3.36673347, 3.38677355,
3.40681363, 3.42685371, 3.44689379, 3.46693387, 3.48697395,
3.50701403, 3.52705411, 3.54709419, 3.56713427, 3.58717435,
3.60721443, 3.62725451, 3.64729459, 3.66733467, 3.68737475,
3.70741483, 3.72745491, 3.74749499, 3.76753507, 3.78757515,
3.80761523, 3.82765531, 3.84769539, 3.86773547, 3.88777555,
3.90781563, 3.92785571, 3.94789579, 3.96793587, 3.98797595,
4.00801603, 4.02805611, 4.04809619, 4.06813627, 4.08817635,
4.10821643, 4.12825651, 4.14829659, 4.16833667, 4.18837675,
4.20841683, 4.22845691, 4.24849699, 4.26853707, 4.28857715,
4.30861723, 4.32865731, 4.34869739, 4.36873747, 4.38877756,
4.40881764, 4.42885772, 4.4488978 , 4.46893788, 4.48897796,
4.50901804, 4.52905812, 4.5490982 , 4.56913828, 4.58917836,
4.60921844, 4.62925852, 4.6492986 , 4.66933868, 4.68937876,
4.70941884, 4.72945892, 4.749499 , 4.76953908, 4.78957916,
4.80961924, 4.82965932, 4.8496994 , 4.86973948, 4.88977956,
4.90981964, 4.92985972, 4.9498998 , 4.96993988, 4.98997996,
5.01002004, 5.03006012, 5.0501002 , 5.07014028, 5.09018036,
5.11022044, 5.13026052, 5.1503006 , 5.17034068, 5.19038076,
5.21042084, 5.23046092, 5.250501 , 5.27054108, 5.29058116,
5.31062124, 5.33066132, 5.3507014 , 5.37074148, 5.39078156,
5.41082164, 5.43086172, 5.4509018 , 5.47094188, 5.49098196,
5.51102204, 5.53106212, 5.5511022 , 5.57114228, 5.59118236,
5.61122244, 5.63126253, 5.65130261, 5.67134269, 5.69138277,
5.71142285, 5.73146293, 5.75150301, 5.77154309, 5.79158317,
5.81162325, 5.83166333, 5.85170341, 5.87174349, 5.89178357,
5.91182365, 5.93186373, 5.95190381, 5.97194389, 5.99198397,
6.01202405, 6.03206413, 6.05210421, 6.07214429, 6.09218437,
6.11222445, 6.13226453, 6.15230461, 6.17234469, 6.19238477,
6.21242485, 6.23246493, 6.25250501, 6.27254509, 6.29258517,
6.31262525, 6.33266533, 6.35270541, 6.37274549, 6.39278557,
6.41282565, 6.43286573, 6.45290581, 6.47294589, 6.49298597,
6.51302605, 6.53306613, 6.55310621, 6.57314629, 6.59318637,
6.61322645, 6.63326653, 6.65330661, 6.67334669, 6.69338677,
6.71342685, 6.73346693, 6.75350701, 6.77354709, 6.79358717,
6.81362725, 6.83366733, 6.85370741, 6.87374749, 6.89378758,
6.91382766, 6.93386774, 6.95390782, 6.9739479 , 6.99398798,
7.01402806, 7.03406814, 7.05410822, 7.0741483 , 7.09418838,
7.11422846, 7.13426854, 7.15430862, 7.1743487 , 7.19438878,
7.21442886, 7.23446894, 7.25450902, 7.2745491 , 7.29458918,
7.31462926, 7.33466934, 7.35470942, 7.3747495 , 7.39478958,
7.41482966, 7.43486974, 7.45490982, 7.4749499 , 7.49498998,
7.51503006, 7.53507014, 7.55511022, 7.5751503 , 7.59519038,
7.61523046, 7.63527054, 7.65531062, 7.6753507 , 7.69539078,
7.71543086, 7.73547094, 7.75551102, 7.7755511 , 7.79559118,
7.81563126, 7.83567134, 7.85571142, 7.8757515 , 7.89579158,
7.91583166, 7.93587174, 7.95591182, 7.9759519 , 7.99599198,
8.01603206, 8.03607214, 8.05611222, 8.0761523 , 8.09619238,
8.11623246, 8.13627255, 8.15631263, 8.17635271, 8.19639279,
8.21643287, 8.23647295, 8.25651303, 8.27655311, 8.29659319,
8.31663327, 8.33667335, 8.35671343, 8.37675351, 8.39679359,
8.41683367, 8.43687375, 8.45691383, 8.47695391, 8.49699399,
8.51703407, 8.53707415, 8.55711423, 8.57715431, 8.59719439,
8.61723447, 8.63727455, 8.65731463, 8.67735471, 8.69739479,
8.71743487, 8.73747495, 8.75751503, 8.77755511, 8.79759519,
8.81763527, 8.83767535, 8.85771543, 8.87775551, 8.89779559,
8.91783567, 8.93787575, 8.95791583, 8.97795591, 8.99799599,
9.01803607, 9.03807615, 9.05811623, 9.07815631, 9.09819639,
9.11823647, 9.13827655, 9.15831663, 9.17835671, 9.19839679,
9.21843687, 9.23847695, 9.25851703, 9.27855711, 9.29859719,
9.31863727, 9.33867735, 9.35871743, 9.37875752, 9.3987976 ,
9.41883768, 9.43887776, 9.45891784, 9.47895792, 9.498998 ,
9.51903808, 9.53907816, 9.55911824, 9.57915832, 9.5991984 ,
9.61923848, 9.63927856, 9.65931864, 9.67935872, 9.6993988 ,
9.71943888, 9.73947896, 9.75951904, 9.77955912, 9.7995992 ,
9.81963928, 9.83967936, 9.85971944, 9.87975952, 9.8997996 ,
9.91983968, 9.93987976, 9.95991984, 9.97995992, 10. ])
x = np.linspace(0, 10, 20) # 10 / 19 = 0.5263157895
x
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316,
2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211,
5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105,
7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ])
np.linspace(1, 10) # defalt 50; (10-1)/(50-1)=9/49=1.18367347
array([ 1. , 1.18367347, 1.36734694, 1.55102041, 1.73469388,
1.91836735, 2.10204082, 2.28571429, 2.46938776, 2.65306122,
2.83673469, 3.02040816, 3.20408163, 3.3877551 , 3.57142857,
3.75510204, 3.93877551, 4.12244898, 4.30612245, 4.48979592,
4.67346939, 4.85714286, 5.04081633, 5.2244898 , 5.40816327,
5.59183673, 5.7755102 , 5.95918367, 6.14285714, 6.32653061,
6.51020408, 6.69387755, 6.87755102, 7.06122449, 7.24489796,
7.42857143, 7.6122449 , 7.79591837, 7.97959184, 8.16326531,
8.34693878, 8.53061224, 8.71428571, 8.89795918, 9.08163265,
9.26530612, 9.44897959, 9.63265306, 9.81632653, 10. ])
x = np.linspace(0, 10, 10)
x
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
x = np.linspace(0, 9, 10)
x
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
x = np.linspace(0, 10, 11)
x
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
x = np.linspace(0, 10, 10)
x
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
x = np.linspace(1, 10, 10)
x
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(1, 10, num=10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(1, 10, 5)
array([ 1. , 3.25, 5.5 , 7.75, 10. ])
np.linspace(-10, 10, 25)
array([-10. , -9.16666667, -8.33333333, -7.5 ,
-6.66666667, -5.83333333, -5. , -4.16666667,
-3.33333333, -2.5 , -1.66666667, -0.83333333,
0. , 0.83333333, 1.66666667, 2.5 ,
3.33333333, 4.16666667, 5. , 5.83333333,
6.66666667, 7.5 , 8.33333333, 9.16666667,
10. ])
range(1, 11)
range(1, 11)
list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
np.linspace(1, 10, 10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
list(range(2, 30, 2))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
np.arange(2, 30, 2)
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
np.linspace(2, 30, 15)
array([ 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26.,
28., 30.])
np.linspace(start=[0, 10, 20], stop=[100, 220, 440], num=11)
array([[ 0., 10., 20.],
[ 10., 31., 62.],
[ 20., 52., 104.],
[ 30., 73., 146.],
[ 40., 94., 188.],
[ 50., 115., 230.],
[ 60., 136., 272.],
[ 70., 157., 314.],
[ 80., 178., 356.],
[ 90., 199., 398.],
[100., 220., 440.]])
np.linspace(start=[0, 10, 20], stop=[100, 220, 440], num=11, axis = 1)
array([[ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.],
[ 10., 31., 52., 73., 94., 115., 136., 157., 178., 199., 220.],
[ 20., 62., 104., 146., 188., 230., 272., 314., 356., 398., 440.]])
np.linspace(start=[0, 10, 20], stop=[100, 220, 440], num=11, axis = 0)
array([[ 0., 10., 20.],
[ 10., 31., 62.],
[ 20., 52., 104.],
[ 30., 73., 146.],
[ 40., 94., 188.],
[ 50., 115., 230.],
[ 60., 136., 272.],
[ 70., 157., 314.],
[ 80., 178., 356.],
[ 90., 199., 398.],
[100., 220., 440.]])
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2, 100)
y = x
plt.plot(x ,y)
[<matplotlib.lines.Line2D at 0x7fd8938b23d0>]
x = np.linspace(-2, 2, 10)
y = x ** 2
plt.plot(x ,y)
[<matplotlib.lines.Line2D at 0x7fe9193468b0>]
x = np.linspace(-2, 2, 100)
y = x ** 2
plt.plot(x ,y)
[<matplotlib.lines.Line2D at 0x7fe91943b1c0>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x ,y)
[<matplotlib.lines.Line2D at 0x7fd893b95a90>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x ,y, 'ro')
[<matplotlib.lines.Line2D at 0x7fd893c8c220>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x ,y, 'bo-')
[<matplotlib.lines.Line2D at 0x7fd893e654f0>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x ,y, 'ro--')
[<matplotlib.lines.Line2D at 0x7fd893ee99a0>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7fe9184f77c0>]
plt.plot(y)
[<matplotlib.lines.Line2D at 0x7fe9186018b0>]
plt.plot(x,y, 'ro')
[<matplotlib.lines.Line2D at 0x7fe918681100>]
plt.plot(x,y, '-ro')
[<matplotlib.lines.Line2D at 0x7fe9187738b0>]
plt.plot(x,y, '--ro')
[<matplotlib.lines.Line2D at 0x7fe918877220>]
plt.plot(x,y, 'bo')
[<matplotlib.lines.Line2D at 0x7fe91896a7c0>]
plt.plot(y, 'r+')
[<matplotlib.lines.Line2D at 0x7fe918a711c0>]
plt.plot(y, 'rs')
[<matplotlib.lines.Line2D at 0x7fe918b69160>]
plt.plot(y, 'rd')
[<matplotlib.lines.Line2D at 0x7fe918c044f0>]
plt.plot(y, 'rs-', linewidth=1, markersize=12)
[<matplotlib.lines.Line2D at 0x7fe918d54760>]
plt.plot(y, 'rs', markersize=12)
[<matplotlib.lines.Line2D at 0x7fe918e519d0>]
plt.plot(x, y, 'rs-', color="green", linewidth=1, markersize=12)
/var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_21923/3510529404.py:1: UserWarning: color is redundantly defined by the 'color' keyword argument and the fmt string "rs-" (-> color='r'). The keyword argument will take precedence. plt.plot(x, y, 'rs-', color="green", linewidth=1, markersize=12)
[<matplotlib.lines.Line2D at 0x7fe918f59a00>]
plt.plot(x, y, 's-', color="green", linewidth=1, markersize=12)
[<matplotlib.lines.Line2D at 0x7fe91905f2e0>]
plt.plot(x, y, 's-', color="green", marker="o", linewidth=1, markersize=12)
/var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_21923/2501196732.py:1: UserWarning: marker is redundantly defined by the 'marker' keyword argument and the fmt string "s-" (-> marker='s'). The keyword argument will take precedence. plt.plot(x, y, 's-', color="green", marker="o", linewidth=1, markersize=12)
[<matplotlib.lines.Line2D at 0x7fe9190f3be0>]
plt.plot(x, y, '-', color="green", marker="o", linewidth=1, markersize=12)
[<matplotlib.lines.Line2D at 0x7fe9191fa370>]
x = np.linspace(-2, 2, 8)
y = x ** 2
plt.plot(x,y,'ro--')
plt.xlabel('x(m)', fontsize=30)
plt.ylabel('y(m)', fontsize=30)
plt.grid()