Python Fundamentals Part 2 – Class Notes
1. Python Character Set
- Definition: A set of valid characters recognized by Python, representing letters, digits, or symbols.
- Character Types:
- Letters: A-Z, a-z
- Digits: 0-9
- Special Symbols: Space, +, -, *, /, (), ~, !, @, #, $, %, ^, &, [, {, ], }, ;, :, ‘, ”, ,, <, ., >, /, ?
- White Spaces: Blank space, Enter, Tab
- Other Characters: Python supports all ASCII and UNICODE characters.
2. Tokens
- Definition: The smallest individual unit in a program.
- Types of Tokens:
- Keywords
- Identifiers (Names)
- Literals
- Operators
- Punctuators
3. Keywords
- Definition: Reserved words in Python with special meaning, used for specific purposes only.
4. Identifiers
- Definition: Names given to variables, objects, classes, functions, etc.
- Rules:
- Can include letters, digits, and underscores.
- Must start with a letter or underscore.
- Case-sensitive (e.g.,
Var
andvar
are different). - Must not be a keyword.
- No spaces or special characters (except underscore).
5. Literals
- Definition: Fixed values used in a program.
- Types of Literals:
- String Literals: Enclosed in single or double quotes (e.g.,
"Python"
,'123456'
). - Numeric Literals: Include integers, floating-point numbers, and complex numbers.
- Boolean Literals:
True
andFalse
. - Special Literals:
None
. - Literal Collections: Collections like lists, tuples, and dictionaries.
- String Literals: Enclosed in single or double quotes (e.g.,
6. String Literals
- Single-Line Strings: Enclosed in quotes and must end on the same line.
- Multi-Line Strings:
- Created using triple quotes (e.g.,
"""Text"""
). - Can span multiple lines.
- Created using triple quotes (e.g.,
7. Non-Graphic (Escape) Characters
- Definition: Special characters that cannot be typed directly (e.g., backspace, tabs).
- Common Escape Sequences:
\\
: Backslash\n
: New line\t
: Horizontal tab\'
: Single quote\"
: Double quote
8. Numeric Literals
- Integer Literals:
- Decimal:
1234
,-50
- Octal: Starts with
0o
(e.g.,0o10
for decimal 8) - Hexadecimal: Starts with
0x
(e.g.,0xF
for decimal 15)
- Decimal:
- Floating Point Literals:
- Fractional Form:
12.0
,-15.86
- Exponent Form:
1.5e2
(represents1.5 × 10^2
)
- Fractional Form:
9. Boolean Literals
- Definition: Used to represent
True
orFalse
.
10. Special Literal: None
- Definition: Represents the absence of a value, similar to
NULL
in other languages.
11. Complex Numbers
- Definition: Numbers with real and imaginary parts (e.g.,
1+0j
). - Accessing Parts: Use
.real
and.imag
attributes (e.g.,x.real
,x.imag
).
12. Type Conversion
- Implicit Type Conversion: Done automatically by Python.
- Explicit Type Conversion: Done by the programmer using functions like
int()
,float()
,str()
, andbool()
.
13. Input in Python
- input() Function: Used to take user input, which is always of
str
type. - Converting Input: Convert input to numeric types using
int()
orfloat()
when needed.
14. Operators
- Definition: Symbols that perform operations on variables and values.
- Unary Operators: Operate on one operand (e.g.,
+
,-
,~
). - Binary Operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
,**
,//
- Example:
python
num1 = 20
num2 = 7
val = num1 % num2 # Remainder is 6
- Arithmetic Operators:
15. Arithmetic Operators
- Addition (
+
): Adds two numbers. - Subtraction (
-
): Subtracts one number from another. - Multiplication (
*
): Multiplies two numbers. - Division (
/
): Divides one number by another, resulting in a float. - Floor Division (
//
): Divides one number by another, discarding the fractional part. - Modulus (
%
): Returns the remainder of a division. - Exponentiation (
**
): Raises one number to the power of another.
16. Relational Operators
- Definition: Compare the values of two operands.
- Types:
- Equal to (
==
): ReturnsTrue
if both operands are equal. - Not Equal to (
!=
): ReturnsTrue
if operands are not equal. - Greater than (
>
): ReturnsTrue
if left operand is greater than the right. - Less than (
<
): ReturnsTrue
if left operand is less than the right. - Greater than or Equal to (
>=
): ReturnsTrue
if left operand is greater than or equal to the right. - Less than or Equal to (
<=
): ReturnsTrue
if left operand is less than or equal to the right.
- Equal to (
- Example:
python
a = 5
b = 10
print(a > b) # Output: False
print(a <= b) # Output: True
17. Logical Operators
- Definition: Used to perform logical operations.
- Types:
- AND (
and
): ReturnsTrue
if both operands are true. - OR (
or
): ReturnsTrue
if at least one operand is true. - NOT (
not
): ReturnsTrue
if the operand is false.
- AND (
- Example:
python
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
18. Assignment Operators
- Definition: Assign values to variables.
- Types:
- Basic Assignment (
=
): Assigns the right-side value to the left-side variable. - Compound Assignment Operators: Combine arithmetic or bitwise operations with assignment.
- Add and Assign (
+=
): Adds right operand to the left operand and assigns the result to the left operand. - Subtract and Assign (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiply and Assign (
*=
): Multiplies the right operand with the left operand and assigns the result to the left operand. - Divide and Assign (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus and Assign (
%=
): Takes the modulus using two operands and assigns the result to the left operand. - Exponentiation and Assign (
**=
): Raises the left operand to the power of the right operand and assigns the result to the left operand. - Floor Division and Assign (
//=
): Performs floor division on operands and assigns the result to the left operand.
- Add and Assign (
- Basic Assignment (
- Example:
python
x = 5
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8
19. Bitwise Operators
- Definition: Perform bit-level operations on integers.
- Types:
- AND (
&
): Bitwise AND. - OR (
|
): Bitwise OR. - XOR (
^
): Bitwise XOR. - NOT (
~
): Bitwise NOT. - Left Shift (
<<
): Shifts bits to the left. - Right Shift (
>>
): Shifts bits to the right.
- AND (
- Example:
python
a = 5 # Binary: 0101
b = 3 # Binary: 0011
print(a & b) # Output: 1 (Binary: 0001)
print(a | b) # Output: 7 (Binary: 0111)
20. Identity Operators
- Definition: Used to compare the memory locations of two objects.
- Types:
is
: ReturnsTrue
if both variables point to the same object.is not
: ReturnsTrue
if variables do not point to the same object.
- Example:
python
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True (both refer to the same list object)
print(x is z) # Output: False (z is a different list object)
21. Membership Operators
- Definition: Used to test if a sequence contains a certain item.
- Types:
in
: ReturnsTrue
if the value is found in the sequence.not in
: ReturnsTrue
if the value is not found in the sequence.
- Example:
python
a = [1, 2, 3, 4, 5]
print(3 in a) # Output: True
print(6 not in a) # Output: True
22. Operator Precedence
- Definition: The order in which operations are performed in an expression.
- Example:
- Multiplication and division are performed before addition and subtraction.
- Parentheses can be used to change the order of operations.
pythonresult = 10 + 2 * 3 # Output: 16 (multiplication first)
result = (10 + 2) * 3 # Output: 36 (parentheses first)
23. Comments in Python
- Definition: Text within the code meant for human readers, ignored by the Python interpreter.
- Types:
- Single-Line Comment: Begins with
#
(e.g.,# This is a comment
). - Multi-Line Comment: Uses triple quotes
"""
or'''
to span multiple lines.
python# This is a single-line comment
"""
This is a
multi-line comment
"""
- Single-Line Comment: Begins with
24. Indentation
- Definition: Python uses indentation to define the scope of loops, functions, classes, and other blocks of code.
- Importance: Indentation is mandatory in Python and affects how the code is executed.
- Example:
python
if True:
print("This is inside the if block")
print("This is outside the if block")