mc8jaad98cyzEpUC53EX5B CS NOTES CLASS 12 PYTHON REVISION TOUR

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Page 1

NITIN PALIWAL
Page 2

CLASS 12 COMPUTER SCIENCE

PYTHON REVISION TOUR


INTRODUCTION TO PYTHON

Python is a high level, general purpose programming language.

ADVANTAGES OF PYTHON:

Readability: Clear and easy-to-understand syntax.


Large Standard Library: Extensive pre-built modules for various tasks.
Versatility: Suitable for web development, data science, and more.
Community Support: Active community providing resources and libraries.
Integration Capabilities: Easily integrates with other languages.
Rapid Development: Quick prototyping and development.
Cross-Platform: Compatible with major operating systems.
Scalability: Can be used in large-scale applications.

DISADVANTAGES OF PYTHON:

Speed: Interpreted nature may be slower than compiled languages.


Global Interpreter Lock (GIL): Limits thread execution on multi-core systems.
Mobile Development: Not the primary choice for mobile app development.
Design Restrictions: Some developers may find design philosophies limiting.
Memory Consumption: May use more memory, especially in resource-constrained
environments.
Threading Limitations: Challenges in leveraging multi-core processors.
Packaging Issues: Managing dependencies can be challenging.
Less Suitable for Resource-Intensive Tasks: Performance may not match
languages designed for resource-intensive tasks.

PYTHON: ADVANTAGES AND DISADVANTAGES

Advantages Disadvantages

1. Readability 1. Speed

2. Large Standard Library 2. Global Interpreter Lock (GIL)

NITIN PALIWAL
Page 3

Advantages Disadvantages

3. Versatility 3. Mobile Development

4. Community Support 4. Design Restrictions

5. Integration Capabilities 5. Memory Consumption

6. Rapid Development 6. Threading Limitations

7. Cross-Platform 7. Packaging Issues

8. Less Suitable for Resource-Intensive


8. Scalability Tasks

TOKENS

TOKENS

KEYWORD IDENTIFIER LITERALS PUNCTUATION OPERATOR

Reserved Symbols Symbols


Words with Names given Constants or used to that
predefined to various fixed values structure perform
meaning in program used in a code and operations
the elements. program. separate on variables
language. statements. and values.

Tokens: The smallest individual unit in a program is known as Token

LITERALS

IDENTIFIERS

KEYWORDS

OPERATORS

IDENTIFIERS NITIN PALIWAL


Page 4

KEYWORDS: RESEVED WORDS

Keyword Description
and Logical operator for conjunction (logical AND).
as Used to create an alias for a module or symbol.
assert Used for debugging to check if a condition is true.
break Terminates the loop prematurely.
class Declares a class in object-oriented programming.
continue Skips the rest of the loop's code and continues to the next iteration.
def Defines a function.
del Deletes an object or a part of an object.
elif Stands for "else if" and is used in conditional statements.
else Specifies a block of code to be executed if the condition is false.
except Catches exceptions during exception handling.
False Boolean value representing false.
finally Specifies a block of code to be executed, regardless of the try-except block.
for Used to iterate over a sequence (e.g., list, tuple, string).
from Used to import specific attributes or functions from a module.
global Declares a global variable.
if Conditional statement, executes code if a specified condition is true.
import Imports a module into the current program.
in Membership test, checks if a value exists in a sequence.
is Compares object identity.
lambda Creates an anonymous function (lambda function).
None Represents the absence of a value or a null value.
not Logical operator for negation (logical NOT).
or Logical operator for disjunction (logical OR).
pass Placeholder indicating no action to be taken.
raise Raises an exception.
return Exits a function and returns a value.
True Boolean value representing true.
try Defines a block of code to be tested for errors.
Creates a loop that executes a block of code as long as a specified
while condition is true.
Simplifies resource management (e.g., file handling) using a context
with manager.
Pauses the execution of a generator function and returns a value to the
yield caller.

NITIN PALIWAL
Page 5

IDENTIFIERS

Rules for Identifiers:

 Must start with a letter (a-z, A-Z) or an underscore (_).


 The remaining characters can be letters, underscores, or digits (0-9).
 Case-sensitive (myVar and myvar are different).

Examples of Identifiers:

Valid:
 my_variable
 count_1
 PI
 _underscore
 Name123
Invalid:
 123abc (starts with a digit)
 my-var (contains a hyphen)
 if (using a Python keyword)
 spaces not allowed
 #hash_symbol (contains a special character)

LITERALS

Single Quotes: 'Hello'


STRING Double Quotes: "World"
Triple Quotes: '''Multi-line string'''

BOOLEAN TRUE AND FALSE

NONE Represents the


absence of a value
LITERALS
Integers: 42, -10
NUMERIC Floats: 3.14, -0.5
Complex Numbers: 2 + 3j

Lists: [1, 2, 3]
LITERAL Tuples: (1, 2, 3)
COLLECTIONS Sets: {1, 2, 3}
Dictionaries: {'key': 'value'}

NITIN PALIWAL
Page 6

PUNCTUATIONS

 Definition: Punctuations are symbols used to structure code and separate


statements in Python.
 Common Punctuations:
 Semicolon (;):
 Separates multiple statements on the same line.
 Comma (,):
 Separates items in a tuple or elements in a list.
 Period (.):
 Accesses attributes or methods of an object.

OPERATORS

Perform mathematical operations like addition,


ARITHMETIC subtraction, multiplication, and division. Example: +, -,
*, /

COMPARISON Compare values and return True or False. Example: ==


(equal), != (not equal), > (greater than)
OPERATORS

LOGICAL Combine conditions and return True or False. Example:


and, or, not

ASSIGNMENT Assign values to variables. Example: =,+=,-=

IDENTITY Check if two objects are the same. Example: is/ is not

MEMBERSHIP Test if a value is a member of a sequence. Example:


in, in not

BITWISE Perform bit-level operations. Example: &, |, ^

NITIN PALIWAL
Page 7

EXECUTING PYTHON PROGRAMS

Printing a simple “Hello Maharathi” Program


INPUT: OUTPUT:

DATA TYPES IN PYTHON

DATA TYPES

NUMBER
1) Integer DICTIONARY
2) Float
3) Complex Number 1) Unordered collection of key-value pairs.
2) Defined by curly braces ({}) with key-value pairs.
3) Example: person = {'name': 'John', 'age': 25}

STRING
1)
2)
Sequences of characters.
Enclosed in single (' '), double
SET
(" "), or triple quotes. 1) Unordered, mutable collection with unique
3) Example: text = "Hello, World!" elements.
2) Defined by curly braces ({}).
3) Example: unique numbers = {1, 2, 3}
BOOLEAN
1) Represents truth values: True or False.
2) Used for logical operations and
conditions.
TUPLE
3) Example: is_true = True 1) Ordered, immutable collection.
2) Defined by parentheses (()).
3) Example: coordinates = (x, y)

LIST
1) Ordered, mutable collection.
2) Defined by square brackets ([]).
3) Example: numbers = [1, 2, 3]

NITIN PALIWAL
Page 8

CONDITIONAL STATEMENTS

If Statement:

 Executes a block of code if a condition is true.

INPUT:

EXAMPLE : Checking if a number is positive with the help of if statement

INPUT:

OUTPUT:

NITIN PALIWAL
Page 9

If-Else Statement:

 Executes one block of code if a condition is true and another if it's false.

INPUT:

EXAMPLE : Checking if a number is positive or negative

INPUT:

OUTPUT:

OUTPUT:

NITIN PALIWAL
Page 10

If-Elif-Else Statement:

 Checks multiple conditions in sequence.


 Executes the block corresponding to the first true condition.

INPUT:

EXAMPLE : Checking the sign of a number

INPUT:

OUTPUT:

NITIN PALIWAL
Page 11

SIMPLE PYTHON PROGRAMS

Absolute Value Program:

 Calculates the absolute value of a number.


 The absolute value of a number is never negative.
 Coverts negative number to positive and positive number remains the same

EXAMPLE : Finding absolute value of -5

INPUT: OUTPUT:

Sort 3 Numbers Program:

 Sorts three numbers in ascending order.

EXAMPLE : Sorting 3 numbers in ascending order

INPUT: OUTPUT:

NITIN PALIWAL
Page 12

Divisibility Check Program:

 Checks if a number is divisible by another number.

EXAMPLE : Checking if 10 is divisible by 2

INPUT:

OUTPUT:

NITIN PALIWAL
Page 13

FOR AND WHILE LOOP

For Loop Program:

 Iterates over a sequence (list, tuple, string, etc.) or a range of values.

EXAMPLE : Calculate the square of numbers using for loop

INPUT:

OUTPUT:

NITIN PALIWAL
Page 14

While Loop Programs:

 Repeats a block of code as long as a condition is true.

EXAMPLE : Calculating the factorial of a number using loop

INPUT:

OUTPUT:

NITIN PALIWAL

You might also like