python unit 1
python unit 1
1. Interactive:
o Python supports interactive mode, where you can write and execute code line
by line using the Python interpreter.
o This feature helps in testing and debugging small pieces of code quickly.
o Example: Running Python commands in a terminal or REPL (Read-Eval-Print
Loop).
2. Object-Oriented:
o Python supports object-oriented programming (OOP), which organizes code
into objects that encapsulate data and behavior.
o Features like classes, inheritance, and polymorphism make it easier to
structure and manage larger programs.
3. Interpreted:
o Python code is executed line by line by the interpreter, which eliminates the
need for compilation.
o This makes development faster and debugging simpler, as errors are detected
immediately during execution.
4. Platform Independent:
o Python programs can run on different platforms (Windows, Mac, Linux)
without modification, as long as the Python interpreter is installed.
o Write once, run anywhere—an essential feature for cross-platform
applications.
5. Ease of Use:
o Python's syntax is simple and easy to understand, making it beginner-friendly.
o The language emphasizes readability, which reduces the learning curve.
1. Identifiers:
o Identifiers are names used to identify variables, functions, or objects in
Python.
o They must begin with a letter (A-Z or a-z) or an underscore (_), followed by
letters or digits.
o Example: myVariable, _privateVar.
2. Keywords:
o Keywords are reserved words in Python with predefined meanings and cannot
be used as identifiers.
o Examples: if, else, while, def, class.
o Python has 35+ keywords (e.g., True, False, import, etc.).
3. Indentation:
o Python uses indentation (whitespace at the beginning of a line) to define
blocks of code.
o Unlike other languages, Python does not use braces {}; proper indentation is
crucial.
o Example:
python
Copy code
if True:
print("Indented block")
4. Variables:
o Variables are used to store data values in Python.
o Python is dynamically typed, so you don’t need to declare variable types
explicitly.
o Example:
python
Copy code
x = 5
name = "Python"
5. Comments:
o Comments are used to explain code and are ignored by the Python interpreter.
o Single-line comments start with #, while multi-line comments use triple quotes
(''' or """).
o Example:
python
Copy code
# This is a single-line comment
'''
This is a
multi-line comment
python
Copy code
print("Welcome to Python!")
Copy code
python welcome.py
o Output:
css
Copy code
Welcome to Python!
1. Numbers:
o Numbers include integers (int), floating-point (float), and complex numbers.
o Example:
python
Copy code
x = 10 # Integer
y = 3.14 # Float
z = 2 + 3j # Complex number
2. String:
o Strings are a sequence of characters enclosed in quotes.
o Example:
python
Copy code
name = "Python"
print(name)
A tuple is like a box where you can store multiple things (numbers, words, etc.) together. But
once you put them in the box, you cannot change them—you can only look at them or use
them.
1. Immutable:
o You cannot add, remove, or change the items inside a tuple after you create it.
o Think of a tuple like a sealed box: you can see and use what’s inside, but you can’t
open it to change anything.
2. How to Create a Tuple:
o Use parentheses () to create a tuple.
o Example:
python
Copy code
my_tuple = (1, 2, 3)
print(my_tuple) # Output: (1, 2, 3)
3. Accessing Items:
o You can access items in a tuple by their position (starting from 0).
o Example:
python
Copy code
print(my_tuple[0]) # Output: 1 (first item)
print(my_tuple[2]) # Output: 3 (third item)
4. Cannot Be Modified:
o If you try to change something in the tuple, Python will give an error.
o Example:
python
Copy code
my_tuple[1] = 10 # Error! Tuples cannot be changed.
Simple Example:
python
Copy code
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
4 list
Lists are mutable (can be changed) and store a collection of items in square brackets.
Example:
python
Copy code
my_list = [1, 2, 3]
python
Copy code
list[index] = new_value
python
Copy code
x[x.index(1)] = 9
5 ) Diractories
A dictionary in Python is like a real dictionary, but instead of words and their meanings, it
stores pairs of "keys" and "values".
Example:
python
Copy code
my_dict = {"name": "Python", "version": 3.10}
Here:
"name" is the key, and "Python" is the value.
"version" is the key, and 3.10 is the value.
python
Copy code
print(my_dict["name"]) # Output: Python
It will print the value associated with the key "name", which is "Python".