Computer Science Final - Python Notes
1. Getting Started with Python
Python is a high-level, interpreted programming language used in various domains like web development, AI,
data science, etc. It's known for its simple syntax and readability, making it ideal for beginners and
professionals alike.
2. Features of Python
- Easy to Learn: Clear syntax.
- Interpreted: Executes line-by-line.
- Dynamic Typing: No need to declare variable types.
- Portable: Works across platforms.
- Extensible Libraries: Huge standard and third-party libraries.
3. Working with Python (Interactive & Script Modes)
- Interactive Mode: Executes one command at a time.
Example: >>> print('Hello')
- Script Mode: Write code in .py file and run using an interpreter.
4. Barebones of a Python Program
- Comments start with #
- Structure:
# This is a comment
print("Hello")
- Indentation is mandatory for blocks (like if, loops).
5. Variables and Assignments
- Syntax: variable_name = value
- No type declaration needed.
- Example:
name = "Arun"
Computer Science Final - Python Notes
age = 16
pi = 3.14
6. Input and Output Statements
- Output: print("Hello")
- Input: name = input("Enter your name: ")
- input() always returns a string. Use int(), float() to convert.
7. Datatypes
- int: 1, 2, 3
- float: 3.14
- str: "Hello"
- bool: True, False
- list, tuple, dict also common.
- Use type() to find datatype.
8. Tokens
Tokens are the smallest units of a program.
- Keywords: if, else, while, for
- Identifiers: variable & function names
- Literals: Numbers, strings
- Operators: +, -, *, /, //, %, **
- Punctuators: (), {}, :, =, etc.
9. Operator Precedence
Order of operations:
1. Parentheses ( )
2. Exponentiation **
3. Multiplication/Division *, /, //, %
4. Addition/Subtraction +, -
Computer Science Final - Python Notes
5. Comparison: >, <, ==, !=
6. Logical: and, or, not
10. Expressions & Evaluating Expressions
- Expression: combination of values and operators.
- Example:
result = (5 + 3) * 2
- Python evaluates and stores the result in a variable.