Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore python101_workbook_v1.0.2

python101_workbook_v1.0.2

Published by ณพิชญ์, 2022-06-26 10:15:49

Description: python101_workbook_v1.0.2

Search

Read the Text Version

Python v1.0.2

Python 005.133

Python



1 5 15 55 121 133



00 : Programming in Engineering Programming Programming 00 : Programming in Engineering 1

Programming Programming 00 : Programming in Engineering 2

Programming 3 Programming Programming 00 : Programming in Engineering

Programming Programming 4 00 : Programming in Engineering

01 : Data Type, Variable and Expression _ int, str, max, sum, abs, ... int -10 0 020) float 5000011 59 str 10.0 1.23e59 'Programming is easy' \"Let's go shopping\" a = b = c = 0.0 ab c 0.0 b5 a = 5; b = 6; a,b = b,a a b a6 x a=x x +- * ** / // % 2 + 1.0 3.0 // 1//2 0 (-1)//2 -1 11//10 1 (-11)//10 11//-10 -2 a = 3+97//2**3%8 a 3+97//8%8 = 3+12%8 = 3+4 = 7 a = 12//3/2+2**3**2 a 12//3/2+2**9 = 12//3/2+512 = 4/2+512 = 2.0+512 = 514.0 a += 2 a = a + 2 a //= 2 a = a // 2 a *= -1 a = a * -1 import math o math.pi, math.e, math.sin(x), math.cos(x), math.sqrt(x), math.log(x,b), ... '12'+'23' '1223' '12'*3 '121212' int float str o int('12') 12 float('1.2') 1.2 str(12//2) '6' str('Aa') 'Aa' / // a/(2*b) a/2/b a/2*b (a/2)*b 01 : Data Type, Variable and Expression 5

print(a,b,c) ab c print(str(a)+str(b)+str(c)) ab c a = input() a a a = input().strip() a a = int(input()) a a = float(input()) o a,b,c = [e for e in input().split()] a,b,c = input().split() o x,y = [int(e) for e in input().split()] o a,b,c = [float(e) for e in input().split()] o f,n = input().split() f = float(f); n = int(n) input().split(' ') input().split() x = input() x y = x**2 + 7 y = (x/2)*a y = x / 2*a + - * / // % ** +- y = x/(2*a) (x**1)/3 (** * / // % y = x**1/3 x y = x**(1/3) * y = 2x + 1 y = 2*x + 1 10e7 1e3 107 10e7 10×107 107 1e7 1000 1e3 1000.0 2345 % 1e2 45.0 6 01 : Data Type, Variable and Expression

++k --k ++k k kk --k k kk import math math y = (-b+math.sqrt(b*b-4*a*c)) / (2*a) math import math y = 2+(x*abs(y-z/2) y = -b+math.sqrt(b*b-4*a*c)) / (2*a) count = 0 Count count Count = count + 1 int = 27 27 print( int ) a = int(input()) int a a print import math a = math.pi * r**2 print('area = '+a) print('area = '+str(a)) print('area =',a) Problem Code y hm s hm s x 01 : Data Type, Variable and Expression 7

Problem Code x a x = (x + a/x)/2 x v1 v2 v3 u1 u2 u3 v x1 y1 x2 y2 x1 y1 x2 y2 r theta x y (r, theta) xy x y r theta (x, y) r theta 8 01 : Data Type, Variable and Expression

Problem Code ab c ab c b ab ca c vo5 vo5vovovovovo Triangle 10.0 area = 50.0 (sq cm) 10 area = 77.162458338772 (sq cm) 90.0 9 1e1 2e1 50.5 01 : Data Type, Variable and Expression

a = input() * area = 1/2*a*b*sin C b = input() C = input() area = 1/2*a*b*sin(C) area = 1/2 a*b*sin C print(area) TypeError: can't multiply sequence by non-int of a = input() type 'float' b = input() C = input() a bC area = 1/2*a*b*sin C print(area) int float a = input() b = input() int float input() C = input() area = 1/2*a*b*sin(C) float print(area) NameError: name 'sin' is not defined a = float(input() sin math.sin b = float(input()) C = float(input()) NameError: name 'math' is not defined area = 1/2*a*b*sin(C) print(area) math import math a = float(input()) b = float(input()) C = float(input()) area = 1/2*a*b*sin(C) print(area) a = float(input()) b = float(input()) C = float(input()) area = 1/2*a*b*math.sin(C) print(area) 10 01 : Data Type, Variable and Expression

import math 10 a = float(input()) 10 b = float(input()) 90 C = float(input()) area = 1/2*a*b*math.sin(C) print(area) 44.699833180027895 50.0 1/2*10*10*math.sin(90) = 1/2*10*10*1 50.0 math.sin(90) math.sin(x) Return the sine of x radians. math.sin math.radians(d) d C C* /180 C*(22/7)/180 C*3.14159/180 math.pi CR = C*math.pi/180 import math 50.0 a = float(input()) b = float(input()) 1e1 C = float(input()) 2e1 CR = C*math.pi/180 50.5 area = 1/2*a*b*math.sin(CR) print(area) 77.162458338772 area = 50.0 (sq cm) import math area = 50.0 (sq cm.) a = float(input()) area = 50.0 (sq cm) b = float(input()) C = float(input()) CR = C*math.pi/180 area = 1/2*a*b*math.sin(CR) print(\"area =\",area, \"(sq cm.)\") import math area = 50.0 (sq cm) a = float(input()) b = float(input()) C = float(input()) CR = C*math.pi/180 area = 1/2*a*b*math.sin(CR) print(\"area =\",area, \"(sq cm)\") 01 : Data Type, Variable and Expression 11

39.85 103.73 313.0 12 01 : Data Type, Variable and Expression

Triangle 2 3 c = 5.0 cm. 4 c = 25.0 cm. 90 c = 9.999999999999998 cm. 7.0 c = 2.9999999999999996 cm. 24.0 90.0 a 10 b 10 D 60 C 3 3 60 D 01 : Data Type, Variable and Expression 13

ISBN 6 020131452 0201314525 100000000 1000000001 14 01 : Data Type, Variable and Expression

02 : Selection ( if-elif-else ) Flowchart Code if C1 : P1 if C1 : if not C1 : pass P1 else : P1 if C1 : P1 else : P2 if C1 : P1 elif C2 : P2 elif C3 : P3 else : P4 02 : Selection ( if-elif-else ) 15

Flowchart Code if C1 : P1 if C2 : P2 if C3 : P3 else : P4 if C1 : if C2 : P2 if C3 : P3 else : P4 else : P1 if C4 : P5 elif C5 : P6 P7 not(x == 0) x != 0 not(x==2 or x==4) x!=2 and x!=4 not(x<2 and y>=4) x>=2 or y<4 3<=x and x<9 3 <= x < 9 a < b and b < c and c < d and d <= e a < b < c < d <= e c=='a' or c=='e' or c=='i' or c=='o' or c=='u' c in ('a', 'e', 'i', 'o', 'u') c in 'aeiou' 16 02 : Selection ( if-elif-else )

if condition : t = value1 if condition else value2 t = value1 t = True if s > a+b%7 else False else : t = (s > a + b % 7) t = value2 if s > a + b % 7 : t = True else : t = False if a%2 == 0 : a if a%100 == 0 : a if (a//100)%10 == 9 : a if (a%1000)//100 == 9: if a <= x <= b : x a b if abs(a-b) <= max(abs(a),abs(b))*1e-10 : ab ab 10-10 mx = a if b > mx : mx = b if c > mx : mx = c if d > mx : mx = d mx a,b,c d max mx = max(a,b,c,d) max(a,b,c,d) a,b,c d = if x = 0 : if x == 0 : and or if x != 2 or x != 3 : if x <= 3 and x == 4 : if else if x > 0 : a = math.sqrt(x) if print(a) 02 : Selection ( if-elif-else ) 17

'abcdegh' < 'b' '1234' < '9' '0' < '9' < 'A' < 'Z' < 'a' < 'z' print(x) 1234 print(y) 9 print( x < y ) True x = '1234', y = '9' print( x < y ) False x = 1234, y = 9 if if d > 0 : a=9 a=9 if d > 0: else c += d - 5 e=c c += d - 5 if-elif-else else: else: a=9 c -= d + 7 c -= d + 7 e=c e=c if s >= 80 : if s >= 80 : g = 'A' g = 'A' elif s >= 70 : elif 70 <= s < 80 : g = 'B' g = 'B' elif s >= 60 : elif 60 <= s < 70 : g = 'C' g = 'C' elif s >= 50 : elif 50 <= s < 60 : g = 'D' g = 'D' else : else : g = 'F' g = 'F' 18 02 : Selection ( if-elif-else )

Problem Code xy touch overlap free (x, y) (x, y) (x, y) xy True False 02 : Selection ( if-elif-else ) 19

Problem Code x a x x3 a Not Found XS S M L XL 20 02 : Selection ( if-elif-else )

a n1 n2 10300 13999 p1 p2 p3 p1 p2 p3 n1 n2 01234 11 811 01000 01250 10075 99999 99 999 99950 99999 10125 19999 13 001 09015 13000 1275 p1,p2,p3,n1,n2 = \\ p1, p2, p3, n1 [int(e) for e in input().split()] n2 s s=0 if n1 <= p1 <= n2 : n1 <= p1 <= n2 s += 10000 01234 11 811 01000 01250 10000 if n1 <= p2 <= n2 : s += 25 if n1 <= p3 <= n2 : s += 100 print(s) 02 : Selection ( if-elif-else ) 21

p1,p2,p3,n1,n2 = \\ 10000 12345 00 000 12000 13000 [int(e) for e in input().split()] 10000 12345 00 000 12345 13000 10000 12345 00 000 12000 12345 s=0 10000 12345 00 000 12345 12345 if n1 <= p1 <= n2 : 0 12345 00 000 12346 14000 0 12345 00 000 12000 12344 s += 10000 print(s) if n1 <= p2 <= n2 p1,p2,p3,n1,n2 = \\ 10000 10099 [int(e) for e in input().split()] s=0 p2 = 50 n1 <= p2 <= n2 #if n1 <= p1 <= n2 : # s += 10000 if n1%100 <= p2 <= n2%100 : s += 25 print(s) if n1%100 <= p2 <= n2%100 00000 50 000 10000 10099 25 00000 50 000 10000 10199 25 50 00000 50 000 10000 10299 25 75 22 02 : Selection ( if-elif-else )

p1,p2,p3,n1,n2 = \\ if n1%100 <= p2 <= n2%100 : s += 25 [int(e) for e in input().split()] n1 n2 s=0 #if n1 <= p1 <= n2 : n//100 n2//100 # s += 10000 if n1//100 == n2//100 : 10000 10099 if n1%100 <= p2 <= n2%100 : 10000 10299 s += 25 102 - 100 = 2 else: 25 s += 25*(n2//100 - n1//100 + 1) 00000 50 000 10000 10199 50 print(s) 00000 50 000 10000 10299 75 p1,p2,p3,n1,n2 = \\ 00000 50 000 10060 10680 [int(e) for e in input().split()] 175 150 s=0 p2 = 50, n1 = 10060 #if n1 <= p1 <= n2 : n2 = 10680 # s += 10000 if n1//100 == n2//100: 1 2 3 10060 - 10600 - if n1%100 <= p2 <= n2%100 : 10099 10100 - 10680 s += 25 3 10599 3 else: 2 00 99 if n1%100 <= p2 : #1 s += 25 if p2 <= n2%100 : #3 s += 25 22 s += 25*((n2//100-1) - \\ # 2 (n1//100+1) + 1) n1%100<=p2 p2<=n2%100 print(s) 60<=50 101 50<=80 105 1 (105-101+1) run, 00000 50 000 10000 10299 75 00000 50 000 10060 10680 run, 150 02 : Selection ( if-elif-else ) 23

p1,p2,p3,n1,n2 = \\ if n//100 == n2//100 [int(e) for e in input().split()] 00000 50 000 10000 10299 s=0 75 #if n1 <= p1 <= n2 : # s += 10000 00000 50 000 10060 10680 150 if n1%100 <= p2 : #1 s += 25 01234 11 811 01000 01250 10075 if p2 <= n2%100 : #3 s += 25 99999 99 999 99950 99999 10125 s += 25*((n2//100-1) - \\ # 2 (n1//100+1) + 1) 19999 13 001 09015 13000 1275 print(s) p1,p2,p3,n1,n2 = \\ [int(e) for e in input().split()] s=0 if n1 <= p1 <= n2 : s += 10000 if n1%100 <= p2 : s += 25 if p2 <= n2%100 : s += 25 s += 25*(n2//100 - n1//100 - 1) if n1%1000 <= p3 : s += 100 if p3 <= n2%1000 : s += 100 s += 100*(n2//1000 - n1//1000 - 1) print(s) 24 02 : Selection ( if-elif-else )

Days in Month 10 2557 31 2 2557 28 4 2556 30 2 2547 29 02 : Selection ( if-elif-else ) 25

ERROR A B+ 87.25 B 69.95 C+ 120 C D+ D F ERROR ERROR A C+ ERROR 26 02 : Selection ( if-elif-else )

70 27 0 7 15 7 10 0 7 16 7 30 30 10 30 7 50 30 10 31 7 200 30 13 31 02 : Selection ( if-elif-else )

Intania 85 CEO & Co-Founder Dek-D Interactive co., ltd. Programming Programming

03 : Repetition ( while, for ) Flowchart Code while True : P4 while True : P1 if C2 : P3 break P2 while True : if C1 : break P2 while True : while C2 : if not C2 : break P2 P2 03 : Repetition ( while, for ) 29

Flowchart Code while C1 : P1 while C2 : P2 P3 while i < j : ij ... i += 2 Code i<j for k in range(n) : i += 2 P1 Flowchart Code Flowchart k=0 while k < n : P1 k += 1 for p in range(m) : for k in range(n) : P1 30 03 : Repetition ( while, for )

Flowchart Code for k in range(m) : P4 P1 if C : P3 break P2 else: P4 else for k = m-1 while C1 : while C1 while P1 if C2 : P3 break P2 else: P4 else P4 range(start, stop, step) start, stop step k = 0, 1, 2, ..., 9 start >= stop for k in range(10) : k = 2, 3, 4, ..., 9 start <= stop for k in range(2,10) : k = 2, 4, 6, 8 for k in range(2,10,2) : k = 10, 8, 6, 4, 2 for k in range(10,1,-2) : for k in range(11,11) : step for k in range(9,10,-1) : step *** break break for i in range(5): # break for j for j in range(6): # break for i if condition1 : break if condition2 : break 03 : Repetition ( while, for ) 31

n for k in range(n) s=0 for k in range(10) : # range(start, stop, step) for k in range(...) a = float(input()) s += a print('average =', (s/10)) q for q for k in range(2, q+1) : # k = 2,3,...,q if q % k == 0 : break # if k == q : print(q,'is prime') else: print(q, '=', k, 'x', q//k) while if break s=0 s=0 n=0 n=0 while True : t = float(input()) while t >= 0 : t = float(input()) if t < 0 : break s += t s += t n += 1 n += 1 t = float(input()) if n == 0 : if n == 0 : print('No Data') print('No Data') else : else : print('avg =',(s/n)) print('avg =',(s/n)) xy z 3 xy 0 n-1 z3 = x2 + y2 3 ( 3 x <= y x<y ) yx for y in range(x,n) for i in range(n) : for j in range(i+1, n) : n = 20 # i<j for x in range(1,n) : # i=j for y in range(x,n): for i in range(n) : t = x**2 + y**2 for j in range(i, n) : z = int(round(t**(1/3),0)) if z**3 == t : print( z,x,y ) 32 03 : Repetition ( while, for )

range for k in range(1,5) k = 1,2,3,4 A q for k in range(2, q) : BA C if q % k == 0 : BC print(q,'is composite') # for k in range(...) : else : # if C : print(q,'is prime') # print('A') else : # print('B') B iscomposite = False for k in range(2,q) : found = False for k in range(...): if q % k == 0 : print(q,'is composite') if C : iscomposite = True print('A') break found = True break if not iscomposite : print(q,'is prime') if not found : for-else print('B') for k in range(2,q) : for-else if q % k == 0 : for k in range(...): print(q,'is composite') break if C : print('A') else : break print(q,'is prime') else: print('B') for k in range(2,q+1): # q+1 q if q % k == 0 : print(q,'is composite') 33 break else : print(q,'is prime') composite s=0 for k in range(1,n): s += k**3 print(s) 03 : Repetition ( while, for )

while s=n=0 while t t = float(input()) while t >= 0 : s += t n += 1 print('avg =', (s/n)) t s=n=0 # t = float(input()) while t >= 0 : s += t n += 1 t = float(input()) print('avg =', (s/n)) while True : # s=n=0 t = float(input()) if t < 0 : break s += t n += 1 print('avg =', (s/n)) for k for k in range(1,5): k ... print(k) # w for m in range(4,10): if m % 3 == 0 : break ... print(m) # break for w in range(10,10): ... print(w) # 34 03 : Repetition ( while, for )

k for k in range(1, 4) : 1 for k in range(...) print(k) 3 k += 2 2 k print(k) # 4 3 while k k while 5 k=1 1 while k < 4 : 3 3 print(k) 5 k += 2 print(k) Problem Code Input k Output k Input k Output k Input Output ab Input Output ab Input Output 35 03 : Repetition ( while, for )

Problem Code Input n n n Output Input n z wxy w3 = x2 + y2 + z2 Output z wxy 45 0.7071067811865475 36045 0.7071067811865475 36 03 : Repetition ( while, for )

import math k for x = math.radians(float(input())) n = 30 n = 30 s s=0 for k in range(n+1): ** # k=0,1,...,n math.factorial s+=(-1)**k*x**(2*k+1) \\ /math.factorial(2*k+1) 45 0.7071067811865475 print(s) n = 100 45 import math OverflowError: int too large to convert to float x = math.radians(float(input())) n = 100 float s=0 float for k in range(n+1): # k=0,1,...,n k print(k) k s+=(-1)**k*x**(2*k+1)\\ /math.factorial(2*k+1) print(s) >>> print(k) 85 >>> print((-1)**k) -1 >>> print(x**(2*k+1)) 1.1491314574538792e-18 >>> print(math.factorial(2*k+1)) 12410180702176678234248405241031039926166055775 01693185388951803611996075221691752992751978120 48758557646495950167038705280988985869071076733 12420322184843643104735778899685482782907545415 61964852153468318044293239598173696899657235903 94761615227855818006117636510842880000000000000 0000000000000000000000000000 >>> float float >>> print(1/math.factorial(2*k+1)) 03 : Repetition ( while, for ) 37

import math x = math.radians(float(input())) n = 100 s=f=x for k in range(1,n+1): f *= (-1)*x**2/((2*k)*(2*k+1)) s += f print(s) k f=x k=0 k=1 f = (-1)*x**2/((2*k+1)*(2*k)) * f = -(x**2)/(3*2) * x k=2 = -(x**3)/(3!) k=3 f = -(x**2)/(5*4) * -(x**3)/(3!) ... = +(x**5)/(5!) f = -(x**2)/(7*6) * (x**5)/(5!) = -(x**7)/(7!) 45 0.7071067811865475 print(k, s) for s k=8 8 x = 180 k = 22 import math s f0 x = math.radians(float(input())) s=f=x for while 1 k=1 while f != 0 while f != 0: 0.7071067811865475 f 0k k=8 f *= (-1)*x**2/((2*k)*(2*k+1)) 1 s += f 45 k += 1 print(k) print(s) 36045 2.541635699930208e+252 38 03 : Repetition ( while, for )

import math 36045 xf x = float(input()) float x = math.radians(x%360) x s=f=x 36045 36045 % 360 = 45 k=1 while f != 0: f *= (-1)*x**2/((2*k)*(2*k+1)) s += f k += 1 print(s) 20 78 39 25 143 30 03 : Repetition ( while, for )

Average until -1 -1 -1 -1 -1 No Data 2295.498850599365 5111.43305555306 8502.139421733784 No Data 515.0100470901091 3705.6829835190097 8722.343211974356 4446.712951812571 6375.086965715 3801.511489785674 7638.911577747659 -1 -1 40 03 : Repetition ( while, for )

30 13 90 41 03 : Repetition ( while, for ) 41

Intania 84 CEO & Co-Founder of Priceza Programming Programming Intania 81 Google Inc. Programming

04 : String 'I am \"Python\".' 'I\\'m Python.' \"I'm Python.\" \"I am \\\"Python\\\".\" '''I'm \"Python\".''' 12 int '12' \"\"\"I'm \"Python\".\"\"\" str(x) x s = \"ABCDEFG\") len(t) t len('') 0 s[0] s[-len(s)] \"A\" s[-1] s[len(s)-1] \"G\" s 0 len(s)-1 -1 -len(s) s[k] \"01234\"[-6] \"01234\"[5] s s[:] s[0:] s[:len(s)] s[::] s[::1] s[::2] \"ACEG\", s[1::2] \"BDF\" s[::-1] s[-1::-1] s[-1:-(len(s)+1):-1] \"GFEDCBA\" s[a:b] ab o \"01234\"[2:50000] \"234\", \"01234\"[4999:50000] \"\" o \"01234\"[-500:-2] \"012\", \"01234\"[-3:-500:-1] \"210\", \"01234\"[-500:-300] \"\" for c in s : sc s = \" Python 3.6 \") t = s.upper() t \" PYTHON 3.6 \" s t = s.lower() t \" python 3.6 \" s t = s.strip() t \"Python 3.6\" s s = s.strip().upper() s \"PYTHON 3.6\" k = s.find(c) cs -1 1 \"engineering\" k = \"engineering\".find(\"ng\") k 1 \"ng\" if c in s if s.find(c) >= 0 k = s.find(c,j) cs j '12'+'23' '1223' '12'*3 '121212' 04 : String 43


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook