Docsity

Python Quick Revision Notes, Summaries of Computer science

Partial preview of the text

Download Python Quick Revision Notes and more Summaries Computer science in PDF only on Docsity!

= Python Py (Pro gramming Language ) ue asy Language Notes 4. What is Python ? ¢ Python is a high-level , interpreted programming language. It is easy to learn and understand. « Python is used to give instructions to the computer. (Simple meat meanin ing ) Python ek programming language hai, jiske through hum computer ko (2: Features ‘of Python} Easy to learn and use. Simple English- like syntax. Free and open-source. Platform independent. Supports object-oriented programming. Used in AI, websites, data science, automation etc. Why Python is Popular ? Clean and simple code. Large community Support. Many libraries available. Good for beginners. Fa structions “dete hain. — © apart Opa _« print() is used to display output. input() is used to take input from the user. name = input("Enter your name: ") print ("Hello”, name) 7. Operators | Operators ore symbols used to perform operations. | I Peranawi¢an | == l= “. Arithmetic (8. Conditional Statements) | Used to make decisions in a program. age = 20 if age >= 18: print tee Eligible”) else: | print (“Not eligible”) 9. Loops) Loops are used to repeat a task. (a) For loop for i in range(1, 6): print (i) Output: 1 2 3 4 5 (b) While loop OO I Petey vps Vane ae _— r 7s a + | Logical and or not Assignment = = ee = 41. Tuples) ® eal tuple is a collection of items, similar to a list but it is immutable, (cannot be changed). numbers = (1, 2,3, 4) print(numbers[1]) # 2 42. Dictionaries, A dictionary stores data in key-value pairs. i student = {"“name": "Ravi", "age": 20} | print ( student["name") # Ravi 1 =1 while { <= 5: print (i) i=l +4 (43. Functions) A function is a reusable block of code. | def add(a, b): return at b | print (add (2, 3)) # 5 | (Types of functions | | Built-in functions (print, input, len) | « User-defined functions | « Lambda functions (small anonymous functions) (44. Modules! | Modules are files that contain \ F . Keys are unique. e Access using key name. (46. File Handling ) Used to read and write data to a file. # Writing to o_file £ = open(“data.txt", "w") f write("Hello Python") f. close() # Reading from a file f = open(“data.txt", "r”) print (. read()) £ close() (0) Python code. Example: import math 47. Error Handling) Use try and except to handle errors, try: num = int (input (" Enter a number: ")) | print (10 / num) ! I except ZeroDivisionError: print (“Cannot divide by zero”) except ValueError: print (“Invalid input") (18) (Practice sstions | | 4. What is the output of 5+3#2? 2. Write a program to find the square of a number, 3. Create a list and print its first 3 elements. | 4. Make a function to find the largest of 3 numbers.