|
| 1 | +# # REVISION ALL PYTHON CONCEPTS AGAIN ! |
| 2 | + |
| 3 | +# # Print function |
| 4 | +# print("Hello") |
| 5 | +# print("Khurram") |
| 6 | + |
| 7 | +# primitive data type |
| 8 | + |
| 9 | +# # String |
| 10 | +# # Anything written inside the quotes is a string |
| 11 | +# name = "Khurram" |
| 12 | +# print(name) |
| 13 | +# print(type(name)) |
| 14 | + |
| 15 | +# # Slicing ,indexing and stepping |
| 16 | +# msg = "hello hoe are you" |
| 17 | +# print(msg[3]) |
| 18 | + |
| 19 | +# print(msg[1:5]) |
| 20 | +# print(msg[::2]) |
| 21 | +# print(msg[1:12:2]) |
| 22 | +# print(msg[-1]) |
| 23 | + |
| 24 | +# reverse string |
| 25 | +# print(msg[::-1]) |
| 26 | + |
| 27 | +# String Functions |
| 28 | +# msg = "hello hoe are you" |
| 29 | +# print(msg.upper()) |
| 30 | +# print(msg.lower()) |
| 31 | +# print(msg.capitalize()) |
| 32 | +# print(msg.title()) |
| 33 | +# print(msg.count("o")) |
| 34 | +# print(msg.find("o")) |
| 35 | +# print(msg.replace("o","a")) |
| 36 | +# print(msg.split(" ")) |
| 37 | +# print(msg.strip()) |
| 38 | +# print(msg.center(50,"*")) |
| 39 | +# print(msg.ljust(50,"*")) |
| 40 | +# print(msg.rjust(50,"*")) |
| 41 | +# print(msg.startswith("h")) |
| 42 | +# print(msg.endswith("u")) |
| 43 | +# print(msg.isalpha()) |
| 44 | +# print(msg.isalnum()) |
| 45 | +# print(msg.islower()) |
| 46 | +# print(msg.isupper()) |
| 47 | +# print(msg.isspace()) |
| 48 | +# print(msg.isdigit()) |
| 49 | +# print(msg.isnumeric()) |
| 50 | +# print(msg.isidentifier()) |
| 51 | +# print(msg.isprintable()) |
| 52 | +# print(msg.istitle()) |
| 53 | +# print(msg.islower()) |
| 54 | +# print(msg.isupper()) |
| 55 | + |
| 56 | + |
| 57 | +# # Types of inverted commas |
| 58 | +# # Single quotes |
| 59 | +# print('hy') |
| 60 | +# print('some one said "hy"') |
| 61 | +# # Double quotes |
| 62 | +# print("bye") |
| 63 | +# print("some one said 'bye'") |
| 64 | +# # Triple quotes |
| 65 | +# print(""" hello |
| 66 | +# world someone "said" """) |
| 67 | + |
| 68 | +# # integer |
| 69 | +# # any number is called integer |
| 70 | +# num = 55 |
| 71 | +# print(type(num)) |
| 72 | + |
| 73 | +# # Float |
| 74 | +# # any decimal number is called float |
| 75 | +# num = 55.55 |
| 76 | +# print(type(num)) |
| 77 | + |
| 78 | +# # Escape sequence |
| 79 | +# # \n - new line |
| 80 | +# print("hello \n world") |
| 81 | +# # \t - tab |
| 82 | +# print("muhammad \t khurram") |
| 83 | +# # \r - carriage return |
| 84 | +# print("hello \r world") |
| 85 | +# # \' \' |
| 86 | +# print("hello \"said\"") |
| 87 | +# print("hello \\ khurram") |
| 88 | + |
| 89 | +# Variables |
| 90 | +# Variables are used to store data |
| 91 | +# you can also say it a container |
| 92 | + |
| 93 | +# name = "Khurram" |
| 94 | +# print(name) |
| 95 | + |
| 96 | +# # Rules OF variables |
| 97 | +# # 1. variable name nt contain number at start |
| 98 | +# 2name = "khurram" |
| 99 | +# # 2. variable name nt contain space |
| 100 | +# first name = " muhammad" |
| 101 | +# # 3 . Variable name nt contain special character |
| 102 | +# name$ = "hy" |
| 103 | +# # 4 . Variable name not use predefined keyword |
| 104 | +# class = "khurram" |
| 105 | +# print = "hy" |
| 106 | + |
| 107 | + |
| 108 | +# Rule to do |
| 109 | + |
| 110 | +# variable name should be meaning full |
| 111 | +# variable name should be short |
| 112 | + |
| 113 | +# number = 10 |
| 114 | +# name = "khurram" |
| 115 | +# age = 18 |
| 116 | + |
| 117 | +# # Input function |
| 118 | +# # input() function is used to take input from user |
| 119 | + |
| 120 | +# user_input = int(input("Enter a number : ")) |
| 121 | +# print("you entered :",user_input) |
| 122 | + |
| 123 | +# IF STATEMENTS |
| 124 | +# IF STATEMENT IS USED TO CHECK THE CONDITION |
| 125 | + |
| 126 | +# i = 10 |
| 127 | +# if i > 10: |
| 128 | +# print("greater") |
| 129 | +# elif i < 10: |
| 130 | +# print("smaller") |
| 131 | +# else: |
| 132 | +# print("equal") |
| 133 | + |
| 134 | +# # NESTED IF |
| 135 | +# # IF STATEMENT INSIDE IF STATEMENT |
| 136 | + |
| 137 | +# marks = 97 |
| 138 | +# if marks > 90: |
| 139 | +# print("A grade") |
| 140 | +# if marks > 95: |
| 141 | +# print("A+ grade") |
| 142 | +# else: |
| 143 | +# print("b grade") |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +# LOOPS |
| 148 | +# FOR LOOP |
| 149 | +# FOR LOOP IS USED TO ITERATE OVER A SEQUENCE |
| 150 | + |
| 151 | +# name = "khurram" |
| 152 | +# for i in name: |
| 153 | +# print(i,end=" ") |
| 154 | + |
| 155 | +# for i in range(0,11,2): |
| 156 | +# print(i) |
| 157 | + |
| 158 | +# # NESTED LOOP |
| 159 | +# for x in range(1,4): |
| 160 | +# for y in range(1,4): |
| 161 | +# print(x,y) |
| 162 | + |
| 163 | +# # WHILE LOOP |
| 164 | +# # WHILE LOOP IS USED TO ITERATE OVER A SEQUENCE |
| 165 | +# score = 0 |
| 166 | +# while score < 5: |
| 167 | +# print("score is",score) |
| 168 | +# score += 1 |
| 169 | + |
| 170 | +# # CONTROL STATEMENTS |
| 171 | +# # BREAK STATEMENT |
| 172 | +# # BREAK STATEMENT IS USED TO TERMINATE THE LOOP |
| 173 | +# for i in range(1,11): |
| 174 | +# print(i) |
| 175 | +# if i == 5: |
| 176 | +# break |
| 177 | + |
| 178 | +# # CONTINUE STATEMENT |
| 179 | +# # CONTINUE STATEMENT IS USED TO SKIP THE CURRENT ITERATION |
| 180 | +# for i in range(1,11): |
| 181 | +# if i == 5: |
| 182 | +# continue |
| 183 | +# else: |
| 184 | +# print(i) |
| 185 | + |
| 186 | +# FUNCTION |
| 187 | +# FUNCTION IS A BLOCK OF CODE THAT CAN BE CALLED SEVERAL TIMES FROM DIFFERENT |
| 188 | +# PARTS OF THE PROGRAM |
| 189 | + |
| 190 | +# def even_odd(x): |
| 191 | +# if x % 2 == 0: |
| 192 | +# return "even" |
| 193 | +# else: |
| 194 | +# return "odd" |
| 195 | + |
| 196 | +# def main(): |
| 197 | +# user_input = int(input("enter a number : ")) |
| 198 | +# print(even_odd(user_input)) |
| 199 | + |
| 200 | +# main() |
| 201 | + |
| 202 | +# # DOC STRING IN FUNCTION |
| 203 | +# # DOC STRING IS USED TO DOCUMENT THE FUNCTION |
| 204 | +# def even_odd(x): |
| 205 | +# """ |
| 206 | +# This function checks whether the number is even or odd |
| 207 | +# Parameters: |
| 208 | +# x (int): The number to be checked |
| 209 | +# Returns: |
| 210 | +# str: "even" if the number is even, "odd" if the number is odd |
| 211 | +# """ |
| 212 | +# if x % 2 == 0: |
| 213 | +# return "even" |
| 214 | +# else: |
| 215 | +# return "odd" |
| 216 | + |
| 217 | +# print(even_odd(int(input("enter a number")))) |
| 218 | + |
| 219 | +# # you can print the doc string of the function |
| 220 | +# print(even_odd.__doc__) |
| 221 | + |
| 222 | + |
| 223 | +# NON PRIMITIVE DATA TYPES |
| 224 | +# LIST |
| 225 | +# LIST IS A COLLECTION OF ITEMS WHICH CAN BE OF ANY DATA TYPE INCLUDING STRINGS, INTE |
| 226 | +# GERS, FLOATS, AND OTHER LISTS |
| 227 | + |
| 228 | +# li = [1,2,3,4,5,"khurram",5.5,True] |
| 229 | +# print(li) |
| 230 | + |
| 231 | +# li = ["k","h","u","r","r","a","m"] |
| 232 | +# my_str = ''.join(li) |
| 233 | +# print(my_str) |
| 234 | + |
| 235 | + |
| 236 | +# # INdexing , SLicing and Stepping |
| 237 | +# print(li[0]) |
| 238 | +# print(li[-1]) |
| 239 | +# print(li[1:3]) |
| 240 | +# print(li[1:]) |
| 241 | +# print(li[:3]) |
| 242 | +# print(li[1:5:2]) |
| 243 | +# print(li[::2]) |
| 244 | +# print(li[::-1]) |
| 245 | + |
| 246 | +# # LIST FUNCTIONS |
| 247 | +# li = [1,2,3,4,5,"khurram",5.5,True] |
| 248 | +# li2 = [1,2,5,8,3,4,5] |
| 249 | +# print(len(li)) |
| 250 | +# print(li.count(5)) |
| 251 | +# print(li.index(5)) |
| 252 | +# li.append(6) |
| 253 | +# print(li) |
| 254 | +# li.insert(2,7) |
| 255 | +# print(li) |
| 256 | +# li.remove(5) |
| 257 | +# print(li) |
| 258 | +# li.pop(2) |
| 259 | +# print(li) |
| 260 | +# li2.sort() |
| 261 | +# print(li2) |
| 262 | +# li2.reverse() |
| 263 | +# print(li2) |
| 264 | +# li.clear() |
| 265 | +# print(li) |
| 266 | +# li.extend([1,2,3]) |
| 267 | +# print(li) |
| 268 | +# li.copy() |
| 269 | +# print(li) |
| 270 | +# index = li.index(2) |
| 271 | +# print(index) |
| 272 | + |
| 273 | + |
| 274 | +# TUPLE |
| 275 | +# TUPLE IS A COLLECTION OF ITEMS WHICH CAN BE OF ANY DATA TYPE INCLUDING STRINGS, |
| 276 | +# INTEGERS, FLOATS, AND OTHER TUPLES |
| 277 | + |
| 278 | +# tu = (1,2,3,4,5,"string") |
| 279 | +# print(tu) |
| 280 | + |
| 281 | +# # indxing , slicing , stepping |
| 282 | +# print(tu[0]) |
| 283 | +# print(tu[-1]) |
| 284 | +# print(tu[1:3]) |
| 285 | +# print(tu[1:]) |
| 286 | +# print(tu[:3]) |
| 287 | +# print(tu[1:5:2]) |
| 288 | +# print(tu[::2]) |
| 289 | +# print(tu[::-1]) |
| 290 | + |
| 291 | +# # Tuple functions |
| 292 | +# tu = (1,2,3,4,5,"string") |
| 293 | + |
| 294 | +# print(len(tu)) |
| 295 | +# print(tu.count(1)) |
| 296 | +# print(tu.index(1)) |
| 297 | + |
| 298 | + |
| 299 | +# DICTIONARY |
| 300 | +# DICTIONARY IS A COLLECTION OF KEY VALUE PAIRS |
| 301 | +# DICTIONARY IS MUTABLE |
| 302 | +# DICTIONARY IS INDEXED BY KEY |
| 303 | + |
| 304 | +# dict = {"name":"John","age":30,"city":"New York"} |
| 305 | +# print(dict) |
| 306 | + |
| 307 | +# print(dict["name"]) |
| 308 | +# print(dict["age"]) |
| 309 | +# print(dict["city"]) |
| 310 | + |
| 311 | +# # loop on dictionary |
| 312 | +# for key , value in dict.items(): |
| 313 | +# print(f"{key} : {value}") |
| 314 | + |
| 315 | +# dict2 = {} |
| 316 | + |
| 317 | +# dict2["a"] = 1 |
| 318 | +# dict2["b"] = 2 |
| 319 | +# dict2["c"] = 3 |
| 320 | + |
| 321 | +# print(dict2) |
| 322 | + |
| 323 | +# # list to dictionary |
| 324 | +# li = [1,2,3,4,5] |
| 325 | +# dict3 = {} |
| 326 | +# for x,y in enumerate(li): |
| 327 | +# dict3[x] = y |
| 328 | +# print(dict3) |
| 329 | + |
| 330 | +# # string to dictionary |
| 331 | +# str = "hello world" |
| 332 | +# dict4 = {} |
| 333 | +# for x,y in enumerate(str): |
| 334 | +# dict4[x] = y |
| 335 | +# print(dict4) |
| 336 | + |
| 337 | + |
| 338 | +# DICTIONARY FUNCTIONS |
| 339 | + |
| 340 | +# dict = {"name":"John","age":30,"city":"New York"} |
| 341 | +# dict2 ={"a":"hello"} |
| 342 | +# print(dict.keys()) |
| 343 | +# print(dict.values()) |
| 344 | +# print(dict.items()) |
| 345 | +# print(dict.get("name")) |
| 346 | +# print(dict.get("age")) |
| 347 | +# print(dict.get("city")) |
| 348 | +# print(dict.get("country")) |
| 349 | +# print(dict.get("country","USA")) |
| 350 | +# print(dict.get("country","USA")) |
| 351 | +# print(dict.setdefault("e","khurram")) |
| 352 | +# print(dict.pop("e")) |
| 353 | +# print(dict.popitem()) |
| 354 | +# print(dict.update(dict2)) |
| 355 | +# print(dict) |
| 356 | +# print(dict.copy()) |
| 357 | +# print(dict.clear()) |
| 358 | +# print(dict.fromkeys("hello")) |
| 359 | +# print(dict.fromkeys("hello",1)) |
| 360 | + |
| 361 | + |
| 362 | +# ERROR HANDLING: |
| 363 | +# try: |
| 364 | +# age = int(input("Enter your age : ")) |
| 365 | +# except Exception: |
| 366 | +# print("Invalid input") |
| 367 | + |
| 368 | +# print(age) |
| 369 | + |
| 370 | +# try: |
| 371 | +# age = int(input("Enter your age: ")) |
| 372 | +# print(age) |
| 373 | +# except Exception: |
| 374 | +# print("Invalid input") |
| 375 | +# else: |
| 376 | +# print("You are an adult") |
| 377 | +# finally: |
| 378 | +# print("Thank you for your input") # This will run always |
| 379 | + |
0 commit comments