|
| 1 | +# comment line in python |
| 2 | +''' |
| 3 | + #Python: It’s All About the Indentation |
| 4 | + indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block. |
| 5 | + https://realpython.com/python-conditional-statements/#python-its-all-about-the-indentation |
| 6 | +
|
| 7 | + if <expr> : |
| 8 | + <stmt> |
| 9 | + <stmt> -- block ends |
| 10 | +else |
| 11 | + <stmt> -- block ends |
| 12 | +
|
| 13 | +while <expr> : |
| 14 | + <block in same indentation> |
| 15 | +
|
| 16 | +''' |
| 17 | +print("test") |
| 18 | +## below code doesn ot work |
| 19 | +''' |
| 20 | +
|
| 21 | +import urllib.request |
| 22 | +webUrl = urllib.request.urlopen('https://www.javatpoint.com/python-tutorial') |
| 23 | +u = urllib.request.urlopen ('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22','dfd', 11) |
| 24 | +u1 = urllib.request.urlopen('https://www.javatpoint.com/python-tutorial') |
| 25 | +from xml.etree.ElementTree import parse |
| 26 | +doc = parse(u) |
| 27 | +for pt in doc.findall('.//pt'): |
| 28 | + print(pt.text) |
| 29 | +
|
| 30 | +''' |
| 31 | +# |
| 32 | +''' |
| 33 | +In interactive mode, The underscore _ holds the last result. Nevre use in a program |
| 34 | +
|
| 35 | +''' |
| 36 | +# |
| 37 | + |
| 38 | +bill_thickness = 0.11 #meters |
| 39 | +tower_height = 99999 #meters |
| 40 | +day = 1 |
| 41 | +num_bills =1 |
| 42 | +while num_bills*bill_thickness < tower_height : |
| 43 | + print(day,num_bills,num_bills*bill_thickness) |
| 44 | + day +=1 |
| 45 | + num_bills *=2 |
| 46 | +print('number of days',day) |
| 47 | +print('number of bills', num_bills) |
| 48 | +print('final height', num_bills*bill_thickness) |
| 49 | + |
| 50 | +''' |
| 51 | +learn few thigns from above example |
| 52 | + types variables comments indentation code block loop case sensitive-python is case sensitive language conditional statement (if else) |
| 53 | + In a print, extra line can be supressed |
| 54 | + The extra newline can be suppressed: |
| 55 | + print('Hello', end=' ') |
| 56 | +print('My name is', 'Jake') |
| 57 | +
|
| 58 | +##user input |
| 59 | +name = input('enter name') |
| 60 | +print('your name is',name) |
| 61 | +
|
| 62 | +##pass statement |
| 63 | +Sometimes you need to specify an empty code block. The keyword pass is used for it. |
| 64 | +
|
| 65 | +if a > b: |
| 66 | + pass |
| 67 | +else: |
| 68 | + print('Computer says false') |
| 69 | +
|
| 70 | +
|
| 71 | +
|
| 72 | +## numbers test |
| 73 | +float |
| 74 | +int |
| 75 | +booleans |
| 76 | +
|
| 77 | +## operators |
| 78 | +x + y Add |
| 79 | +x - y Subtract |
| 80 | +x * y Multiply |
| 81 | +x / y Divide (produces a float) |
| 82 | +x // y Floor Divide (produces an integer) |
| 83 | +x % y Modulo (remainder) |
| 84 | +x ** y Power |
| 85 | +x << n Bit shift left |
| 86 | +x >> n Bit shift right |
| 87 | +x & y Bit-wise AND |
| 88 | +x | y Bit-wise OR |
| 89 | +x ^ y Bit-wise XOR |
| 90 | +~x Bit-wise NOT |
| 91 | +abs(x) Absolute value |
| 92 | +
|
| 93 | +## comparison |
| 94 | +x < y Less than |
| 95 | +x <= y Less than or equal |
| 96 | +x > y Greater than |
| 97 | +x >= y Greater than or equal |
| 98 | +x == y Equal to |
| 99 | +x != y Not equal to |
| 100 | +
|
| 101 | +## Converting Numbers |
| 102 | +a = int(x) # Convert x to integer |
| 103 | +b = float(x) # Convert x to float |
| 104 | +
|
| 105 | +>> solve dave's mortagage |
| 106 | +
|
| 107 | +''' |
| 108 | +principal = 500000.0 |
| 109 | +rate = 0.05 |
| 110 | +overperiod = 30 |
| 111 | +payment = 2684.11 |
| 112 | +total_paid = 0.0 |
| 113 | +interest = 0.0 |
| 114 | +month = 1 |
| 115 | +extraPaid = 1000 |
| 116 | +forMonths = 12 |
| 117 | +while principal > 0: |
| 118 | + if forMonths > 1: |
| 119 | + principal = principal - extraPaid |
| 120 | + forMonths -=1 |
| 121 | + principal = principal * (1 +rate/12 ) - payment |
| 122 | + total_paid = total_paid + payment |
| 123 | + print ('month - principal', month, principal) |
| 124 | + month +=1 |
| 125 | + |
| 126 | +print('total paid', total_paid) |
| 127 | + |
| 128 | +print('hello') |
| 129 | + |
| 130 | +''' |
| 131 | +## Strings |
| 132 | +## strings are immutable in python |
| 133 | +
|
| 134 | +''' |
| 135 | +a = "hello world" |
| 136 | +x = a[2] |
| 137 | +x = x+ "hello" |
| 138 | +print(x) |
| 139 | + |
| 140 | +'reverse string' |
| 141 | +name = "prakash" |
| 142 | +count = len(name) |
| 143 | +print(count/2) |
| 144 | +i =0 |
| 145 | +name1 = '' |
| 146 | +while i < round(len(name)): |
| 147 | + name1 = name1 + name[count-i-1] |
| 148 | + i +=1 |
| 149 | + |
| 150 | +print(name) |
| 151 | +print(name1) |
| 152 | + |
| 153 | +name = 'zyxmo' |
| 154 | +name1 ='' |
| 155 | +for x in reversed(name): |
| 156 | + name1 +=x |
| 157 | + |
| 158 | +print(name1) |
| 159 | + |
| 160 | +''' |
| 161 | +## raw strings |
| 162 | +''' |
| 163 | +rs = r'c:\newdata\test' |
| 164 | +rs1 = 'c:\newdata\test' |
| 165 | +print(rs) |
| 166 | +print(rs1) |
| 167 | + |
| 168 | +## byte tring |
| 169 | +bstr = b'hello' |
| 170 | +len(bstr) |
| 171 | +bstr1 = 'hello' |
| 172 | +len(bstr1) |
| 173 | +money = 1001.212 |
| 174 | +## formatted string printing |
| 175 | +print(f'{name:10s} {money:5.2f}') |
| 176 | + |
| 177 | +## excercises |
| 178 | +symbols = 'AAPL,IBM,MSFT,YHOO,SCO' |
| 179 | +symbols[0] |
| 180 | +symbols += ',GOOG' |
| 181 | +print(symbols) |
0 commit comments