Python Notes
Python Notes
First Program, Character Set, Variables and Their Usage, Rules for Identifiers, Data Types, and
Operators.
Simple and Readable Syntax: Python’s syntax is clear and resembles English, reducing
the learning curve.
Interpreted Language: Code is executed line-by-line, eliminating the need for
compilation.
Cross-Platform: Runs on Windows, macOS, Linux, and more.
Extensive Standard Library: Provides modules and packages for various tasks.
Dynamically Typed: No need to declare variable types explicitly.
Open-Source: Free to use and distribute with a large community for support.
python
Copy
# First Python program
print("Hello, World!")
Explanation:
Python source code is written in plain text, and the interpreter processes these characters to
execute the program.
Syntax:
python
Copy
variable_name = value
Example:
python
Copy
# Declaring variables
name = "Alice" # String
age = 25 # Integer
height = 5.5 # Float
# Using variables
print(name, "is", age, "years old and", height, "feet tall.")
Key Points:
Dynamic Typing: No need to specify the data type; Python infers it (e.g., age = 25
makes age an integer).
Reassignment: Variables can be reassigned to different types (e.g., age = "twenty-five"
changes age to a string).
Memory Allocation: Variables reference memory locations, and Python’s garbage
collector manages memory.
Variable Scope: Variables can be local (inside a function) or global (defined outside).
1. Allowed Characters:
o Letters (A-Z, a-z), digits (0-9), and underscore (_).
o Example: my_variable, count_1, MyClass.
2. First Character:
o Must start with a letter or underscore, not a digit.
o Valid: name, _temp, Class1.
o Invalid: 1name, 2count.
3. Case Sensitivity:
o Python is case-sensitive (e.g., Name and name are different).
4. No Reserved Words:
o Cannot use Python keywords (e.g., if, for, while, class, return) as identifiers.
o Full list of keywords: False, None, True, and, as, assert, async, await, break, class,
continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is,
lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.
5. No Special Characters:
o Symbols like @, $, %, etc., are not allowed (except _).
o Invalid: my@var, price$.
6. Length:
o No strict limit, but keep identifiers meaningful and concise (e.g., student_name is
better than s).
1. Numeric Types:
o int: Integer numbers (e.g., 42, -10).
o float: Floating-point numbers (e.g., 3.14, -0.001).
o complex: Complex numbers (e.g., 3 + 4j).
2. Sequence Types:
o str: Strings, immutable sequences of characters (e.g., "Hello", 'World').
o list: Ordered, mutable collections (e.g., [1, 2, 3], ["apple", "banana"]).
o tuple: Ordered, immutable collections (e.g., (1, 2, 3), ("a", "b")).
3. Mapping Type:
o dict: Key-value pairs (e.g., {"name": "Alice", "age": 25}).
4. Set Types:
o set: Unordered, mutable collections of unique elements (e.g., {1, 2, 3}).
o frozenset: Immutable version of a set (e.g., frozenset([1, 2, 3])).
5. Boolean Type:
o bool: Represents True or False.
6. None Type:
o NoneType: Represents the absence of a value (e.g., None).
python
Copy
x = 42
print(type(x)) # Output: <class 'int'>
Type Conversion: Convert between types using functions like int(), float(), str(), etc.:
python
Copy
num_str = "123"
num_int = int(num_str) # Converts to 123 (integer)
Operators in Python
Operators are symbols that perform operations on variables and values. Python supports the
following categories:
1. Arithmetic Operators:
o +: Addition (e.g., 5 + 3 = 8).
o -: Subtraction (e.g., 5 - 3 = 2).
o *: Multiplication (e.g., 5 * 3 = 15).
o /: Division (e.g., 5 / 2 = 2.5).
o //: Floor division (e.g., 5 // 2 = 2).
o %: Modulus (remainder) (e.g., 5 % 2 = 1).
o **: Exponentiation (e.g., 2 ** 3 = 8).
2. Comparison Operators:
o ==: Equal to (e.g., 5 == 5 is True).
o !=: Not equal to (e.g., 5 != 3 is True).
o >: Greater than (e.g., 5 > 3 is True).
o <: Less than (e.g., 3 < 5 is True).
o >=: Greater than or equal to.
o <=: Less than or equal to.
3. Logical Operators:
o and: True if both operands are true (e.g., True and False is False).
o or: True if at least one operand is true (e.g., True or False is True).
o not: Negates the truth value (e.g., not True is False).
4. Assignment Operators:
o =: Assigns a value (e.g., x = 5).
o +=: Adds and assigns (e.g., x += 3 is x = x + 3).
o -=, *=, /=, //=, %=, **=: Similar compound assignments.
5. Bitwise Operators:
o &: Bitwise AND (e.g., 5 & 3 is 1).
o |: Bitwise OR (e.g., 5 | 3 is 7).
o ^: Bitwise XOR (e.g., 5 ^ 3 is 6).
o ~: Bitwise NOT (e.g., ~5 is -6).
o <<: Left shift (e.g., 5 << 1 is 10).
o >>: Right shift (e.g., 5 >> 1 is 2).
6. Membership Operators:
o in: Checks if a value is in a sequence (e.g., 3 in [1, 2, 3] is True).
o not in: Checks if a value is not in a sequence.
7. Identity Operators:
o is: Checks if two variables refer to the same object (e.g., x is y).
o is not: Checks if they are different objects.
Example:
python
Copy
x = 10
y = 3
print(x + y) # Output: 13
print(x > y) # Output: True
print(x in [5, 10, 15]) # Output: True