unit 1 b)
unit 1 b)
1. Introduction to Python
• Interpreted Language – Python executes code line by line (no compilation required).
2. Python Interpreter
The Python Interpreter is responsible for executing Python code. It works in two modes:
o Example:
o Hello, Python!
2. Script Mode
o Write Python programs in a file (.py) and execute them using the interpreter.
o Example: Save the following in script.py and run it using python script.py
o print("Hello, Python!")
What is a Value?
• A value is any piece of data that a program manipulates.
• Example:
• 42 # Integer value
4. Python Numbers
2. a = 10
4. b = 3.14
6. c = 2 + 3j
8. d = True
5. Python Strings
Example:
str1 = "Hello"
str2 = "Python"
print(str1 * 3) # Repetition
Output:
Hello Python
HelloHelloHello
6. Lists in Python
List Operations:
Method Description
Example:
fruits.append("orange")
Example:
print(coordinates[0]) # Output: 10
8. Dictionaries in Python
Example:
9. Variables in Python
Example:
x = 10
name = "Alice"
print(x, name)
Example:
_valid_name = 10 # Valid
2name = "Alice" # Invalid (Cannot start with a digit)
Example:
import keyword
What is an Expression?
• Produces a result.
Example:
result = 10 + 5 # Expression
What is a Statement?
Example:
x = 5 # Assignment statement
print("Hello")
Types of Operators:
Example:
x=5
y = 10
• Identity Operators (is, is not) – Check if two variables refer to the same object.
Example:
x = [1, 2, 3]
y=x
print(x is y) # True
print(2 in x) # True
Python provides several built-in data types to store different kinds of values.
Example:
x = 10 # Integer
y = 3.14 # Float
z = "Hello" # String
Hello, Python!
>>> 2 + 3
2. Script Mode
Example (script.py):
print("Hello, Python!")
Run using:
python script.py
Output:
Hello, Python!
Function in Python
Example:
def greet(name):
print(f"Hello, {name}!")
Module in Python
return a + b
import mymodule
Comparison Operators Compare two values ==, !=, >, <, >=, <=
Identity Operators Check if two variables reference the same object is, is not
Examples:
# Arithmetic Operators
a = 10
b=3
print(a + b) # Output: 13
# Comparison Operators
x = True
y = False
# Membership Operator
# Identity Operator
a = [1, 2, 3]
b=a