Uploaded by Hemali P Patel

Python

advertisement
Use console on right to check ideas
To Use exponents, do 2**5 and it will give 32
Can do print statements by saying print(“hello world”)
Can print x and set values for x and change its value
Floating points: E represents 10 to the power of blank ex 3e2 is 3*10^2
Called Screens: S=” hello! “
Then print s and it will give hello!
Boolean has true false statements, if statements
Python unlike other langs can overwrite values in lang when you reset the value but try to
avoid
List is a collection of elements : do L= [1,2,3,4,]
List doesn’t have to have the same type of value can be [1,True,r,5]
To set value of first element use L2=[0], next value is L2[1]
Get length by doing len(L2) to print last element would have to do L2[len(L2)-1]
Left side lets you write more complex code
Code
Data = [1,2,43,7,-10.100]
Print(“Length of data is “ + str(len(data)))
+ puts strings together
Str converts data to a string
In a for loop it knows if its in a loop according to indentation and using a colon :
For loop in list:
“do something”
For item in data :
Print(“Items with value” + str(item))
Print(“Done with item”)
Print(“length of data is “ + str(len(data)))
For I in range(len(data)):
Print(“Item” + str(i) “ with value” + str(data[i]))
Print(“done with item”)
If statements
If “ logical Expression is true” :
“execute here”
else:
“this will execute instead if above false”
Ex For I in range(len(data)):
Print(“Item” + str(i) “ with value” + str(data[i]))
Print(“done with item”)
To find max value in data do
Print(max(data)) can do same for min
Ex “find min of sin(1), sin(2),…,sin(20)
Import math
bestY= float(“inf”)
for x in range(21) :
y = math.sin(x)
if y < bestY:
best Y= y
bestX= x
Print (“best x “ + str(bestX))
Print(“best y” + str(bestY))
Floating points represent that a value can go to infinity
Import is to use stuff from the math package such as math.sin
To fine max u’d
Import math
bestY=- float(“inf”)
for x in range(21) :
y = math.sin(x)
if y > bestY:
best Y= y
bestX= x
Print (“best x “ + str(bestX))
Print(“best y” + str(bestY))
Import numpy
X= numpy.array([1,2,43,7,-10,100])
Print x
Numpy is like a vector and can find matrix
A = numpy.arrary([1,2,3},[4,5,6],[7,8,9]])
Print A
TO print elements can do
Print(x[0])
Numpy.zeros(10) makes a list of zeros can do same with any number like numpy.ones(10)
Can make arrays by doing numpy.zeros[3,4]) makes a matrix with three rows and 4 columns
Package called random to make random nums
Import random
Import numpy
“creats a matrix with randome entries
# lets say it has entries drawn from the normal
# distribution
X= numpy.zero([3,4])
For I in range(3):
For j in range(4):
X[I,j] = random.normalvariate(0,1)
Print(x)
In the for loop it runs as I = 0 loop j = 1,2,3,
Then I = 1 loop j= 1,2,3
Then i=2 loop j = 1,2,3
Download