We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11
PYTHON REVISION 5) Integra on Capabili es:
Easily integrates with other
TOUR FOR CLASS 12 languages. 6) Rapid Development: Quick prototyping and GETTING STARTED WITH development. PYTHON 7) Cross-Pla orm: Compa ble with major opera ng systems. Introduc on to Python: 8) Scalability: Can be used in Python is an interpreted, large-scale applica ons. high-level, and general- purpose programming Disadvantages of Python: language. It emphasizes code readability 1) Speed: Interpreted nature with its notable use of may be slower than compiled significant indenta on. languages. 2) Global Interpreter Lock (GIL): Features of Python: Limits thread execu on on Easy to learn and use. mul -core systems. Open-source and free. 3) Mobile Development: Not the Extensive library support. primary choice for mobile app Pla orm-independent. development. 4) Design Restric ons: Some Advantages of Python: developers may find design philosophies limi ng. 1) Readability: Clear and easy- 5) Memory Consump on: May to-understand syntax. use more memory, especially 2) Large Standard Library: in resource-constrained Extensive pre-built modules environments. for various tasks. 6) Threading Limita ons: 3) Versa lity: Suitable for web Challenges in leveraging development, data science, mul -core processors. and more. 7) Packaging Issues: Managing 4) Community Support: Ac ve dependencies can be community providing challenging. resources and libraries. 8) Less Suitable for Resource- Intensive Tasks: Performance may not match languages 3) Literals: designed for resource- Fixed values in a program. intensive tasks Types of literals: Numeric Literals: TOKENS Integers, floats, complex numbers. Introduc on to Tokens: x = 10 Tokens are the smallest units y = 3.14 of a Python program. They z = 2 + 3j are the building blocks of a i. String Literals: Python script. s = "Hello" ii. Boolean Literals: Types of Tokens: True and False. 1) Keywords: status = True Reserved words in Python 4) Operators: that have predefined Operators are special symbols or meanings. keywords in Python that perform Examples: if, else, while, operations on operands. Operands for, import, True, False. are the values or variables on which if True: the operation is performed. Python print("This is a keyword example") supports a wide range of operators classified into several categories. 2) Iden fiers: Names used to iden fy Types of Operators: variables, func ons, a) Arithmetic Operators: classes, etc. These operators are used to perform Must begin with a le er mathematical (A-Z or a-z) or an operations. underscore (_), followed Operato by le ers, digits (0-9), or Description Example r underscores. + Addition 5+3→8 Examples: my_var, _temp, - Subtraction 10 - 4 → 6 Counter. * Multiplication 6 * 3 → 18 my_var = 10 / Division 15 / 2 → 7.5 print(my_var) // Floor Division 15 // 2 → 7 Modulus % 15 % 4 → 3 (Remainder) Operato used to assign values to Description Example r variables. 2 * * Exponentiatio 3 Operator Description Example ** n → = Assign x = 10 8 x += 5 → += Add and assign 15 b) Relational x -= 3 → (Comparison) -= Subtract and assign 12 Operators: These x *= 2 → *= Multiply and assign operators compare two 24 values and return a /= Divide and assign x /= 4 → 6 Boolean (True or False). Floor divide and //= x //= 2 → 3 Operator Description Example assign 5 == 5 → Exponent and == Equal to **= x **= 2 → 9 True assign 5 != 3 → Modulus and != Not equal to %= x %= 2 → 1 True assign 7>5→ > Greater than True e) Bitwise Operators: 3<5→ < Less than These operators True Greater than or 5 >= 5 → perform operations at >= equal to True the bit level. Less than or equal 4 <= 5 → Operator Description Example <= to True & AND 5&3→1 ` ` OR c) Logical Operators: ^ XOR 5^3→6 These operators are ~ NOT ~5 → -6 used to combine << Left Shift 5 << 1 → 10 conditional statements. 5 >> 1 → Operator Description Example >> Right Shift 2 Returns True if (5 > 3) and (6 and both are true > 4) → True f) Membership Returns True if (5 > 3) or (3 > Operators These or one is true 7) → True operators test Reverses the not(5 > 3) → membership in a not Boolean result False sequence (e.g., list, string). d) Assignment Operators: These operators are Operator Description Example 'a' in 'apple' → Strings in True if present True Introduc on: True if not 'x' not in not in Strings in Python are present 'apple' → True sequences of characters g) Identity Operators enclosed in quotes. These operators s = "Hello" compare memory locations of two String Opera ons: objects. Operator Description Example Concatena on: is True if same object x is y "Hello" + " World" is not True if not the same x is not # Output: "Hello World" object y Repe on: Python Execu on Modes: "Hi " * 3 Interac ve Mode: Commands # Output: "Hi Hi Hi " are executed one at a me in a Python shell. Membership: Script Mode: A program is 'a' in 'apple' wri en in a file with .py # Output: True extension and executed. String Slicing: Basic Syntax: Slicing allows you to extract por ons of a string using Comments: indices. # This is a comment Syntax: string[start:stop:step] Variables: x = 10 Examples: y = "Hello" s = "Hello, World!" Example Program: print("Hello, Python!") print(s[1:5]) x=5 # Output: "ello" print(x * 2) # Output: 10 print(s[:5]) # Output: "Hello" (start is omi ed, upper(): Converts the string default is 0) to uppercase. print("hello".upper()) print(s[7:]) #Output: "HELLO" # Output: "World!" (stop is omi ed, goes ll the end) strip(): Removes leading and trailing spaces. print(s[::2]) print(" hello ".strip()) # Output: "Hlo ol!" (every second # Output: "hello" character) replace(): Replaces a print(s[::-1]) substring with another # Output: "!dlroW ,olleH" (reverses substring. the string) print("apple".replace("a", "A")) # Output: "Apple" Nega ve Indexing: Nega ve indices count from the find(): Returns the index of end of the string. the first occurrence of a print(s[-1]) substring. # Output: "!" (last character) print("banana".find("na")) # Output: 2 print(s[-6:-1]) # Output: "World" (slicing with Example Program: nega ve indices) s = "Hello, World!" print(s.lower()) Built-in String Methods:
len(): Returns the length of Lists
the string. Introduc on: print(len("Hello")) A list is a collec on of items, # Output: 5 which can be of different data types. lower(): Converts the string to lowercase. lst = [1, 2, "Apple", 3.5] print("HELLO".lower()) # Output: "hello" List Opera ons: Concatena on: [1, 2] + [3, 4] insert(): Inserts an element at # Output: [1, 2, 3, 4] a specified posi on. lst.insert(2, "New") Repe on: ["Hi"] * 3 remove(): Removes the first # Output: ["Hi", "Hi", "Hi"] occurrence of a value. lst.remove(3) Membership: 3 in [1, 2, 3] pop(): Removes and returns # Output: True the element at the specified posi on. List Slicing: lst.pop(1) Similar to string slicing. lst = [1, 2, 3, 4, 5] sort(): Sorts the list in ascending order. print(lst[1:4]) lst.sort() # Output: [2, 3, 4] reverse(): Reverses the order print(lst[:3]) of elements in the list. # Output: [1, 2, 3] lst.reverse()
append(): Adds an element to the end of the list. lst.append(6)
extend(): Adds elements of Tuples
another list. lst.extend([7, 8]) Introduc on: A tuple is a collec on of print((1, 2, 1).count(1)) immutable items. # Output: 2 tpl = (1, 2, "Apple") index(): Returns the index of Tuple Opera ons: the first occurrence of a Concatena on: value. (1, 2) + (3, 4) print((1, 2, 3).index(2)) # Output: (1, 2, 3, 4) # Output: 1
Repe on: Example Program:
("Hi",) * 3 tpl = (1, 2, 3) # Output: ("Hi", "Hi", "Hi") print(tpl.index(2)) # Output: 1 Membership: 3 in (1, 2, 3) # Output: True Dic onary Tuple Slicing: Introduc on: Similar to lists. A dic onary is a collec on of tpl = (1, 2, 3, 4, 5) key-value pairs.