Python Examples
Python Examples
SWITCHING
OBJECTIVE
Familiarization with the Pycharm programming environment and first steps with the syntax of
programming in Python.
ACTIVITY
2) ______________________________________________________________________
#!/usr/bin/env python
3)_______________________________________________________________________
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import sqrt, asin, pi
def area_triangle(a, b, c):
s = (a + b + c) / 2.0
returnsqrt(s * (s - a) * (s - b) * (s - c))
defangulo_alfa(a, b, c):
s = triangle_area(a, b, c)
return 180 / pi * asin(2.0 * s / (b * c))
defmenu():
opcion =0
while option != 1 and option != 2:
1) Calculate the area of the triangle
2) Calculate the angle opposite the first side
option = int(input('Choose option: '))
return option
side1 = float(input('Give me side a: '))
side b:
float(input('Give me side c: '))
s = menu()
ifs == 1:
result = triangle_area(side1, side2, side3)
else:
alpha_angle(side1, side2, side3)
You chose the option, s
print 'The result is:', result
4)___________________________________________________________________________
#!/usr/bin/env python
carga50 =1000
carga20 =1000
carga10 =1000
def withdraw_money(amount):
globalcarga50, carga20, carga10
if amount <= 50 * load50 + 20 * load20 + 10 * load10:
de50 = quantity / 50
amount = amount % 50
ifde50 >= carga50:
amount = amount + (from50 - load50) * 50
charge50
amount / 20
amount = amount % 20
ifde20 >= carga20:
amount = amount + (from20 - load20) * 20
load20
de10 = quantity / 10
amount = amount %10
ifde10 >= carga10:
amount = amount + (from10 - load10) * 10
load10
if quantity == 0:
5) ______________________________________________________________________________
#!/usr/bin/env python
6)________________________________________________________________________________
i = 0
while i < 7:
print
i += 1
Done
for i in range(1,6):
printi
#!/usr/bin/env python
9)_____________________________________________________________________________
10)_____________________________________________________________________________
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def null_matrix(rows, columns):
M = []
for in range(rows):
M.append( [0] * columns )
returnM
deflee_matrix(rows, columns):
M = null_matrix(rows, columns)
foriinrange(rows):
for j in range(columns):
M[i][j] = float(input('Enter the component (%d,%d): ' % (i, j)))
returnM
this is a comment
defdimension(M):
return[len(M),len(M[0])]
defsuma(A, B):
if dimension(A) != dimension(B):
returnNone
else:
[m, n] = dimension(A)
C = null_matrix(m, n)
foriinrange(m):
for in range(n):
C[i][j] = A[i][j] + B[i][j]
returnC
a = input('Enter number of rows')
b = input('Enter number of columns')
C = null_matrix(a,b)
D = null_matrix(a,b)
C = read_matrix(a, b)
D = read_matrix(a, b)
E = sum(C,D)
printC
printD
printE
11)__________________________________________________________________________
defmin(a, b):
if a < b:
return
else:
returnb
defmax(a, b):
ifa > b:
return
else:
returnb
if __name__ == '__main__':
The maximum of 3 and 10 is 10
The maximum of 3 and -10 is 3
print 'The minimum of 3 and 10 is', min(3,10)
print 'The minimum of 3 and -10 is', min(3,-10)