0% found this document useful (0 votes)
3 views

python unit 1

Uploaded by

Mitesh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python unit 1

Uploaded by

Mitesh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

1 Features of Python – Interactive, Object-oriented, Interpreted, Platform


Independent

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.2 Python Building Blocks – Identifiers, Keywords, Indentation, Variables, Comments

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

Explanation of 1.4 and 1.5:

1.4 Running Simple Python Scripts to Display a 'Welcome' Message

1. Write a Python Script:


o A Python script is a file containing Python code with a .py extension.
o Example: Create a file named welcome.py and write the following code:

python
Copy code
print("Welcome to Python!")

2. Save the File:


o Save your file with a .py extension, for example, welcome.py.

3. Run the Script Using the Python Interpreter:


o Open a terminal or command prompt.
o Navigate to the folder where you saved the script and run:

Copy code
python welcome.py
o Output:

css
Copy code
Welcome to Python!

4. Use IDE or Code Editors:


o Python scripts can also be run directly from an Integrated Development Environment
(IDE) like PyCharm, VS Code, or IDLE.
o Example: Press the "Run" button in your IDE to execute the script.

5. Interactive Mode vs. Script Mode:


o In interactive mode, you can type print("Welcome to Python!") directly in the
Python shell to get immediate output.
o In script mode, you save the code in a file and run the file for output.

1.5 Python Data Types: Numbers, String, Tuples, Lists, Dictionary

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)

3 Tuples in Simple Words

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.

Key Points About Tuples:

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.

5. Why Use Tuples?


o Tuples are useful when you want to store things that should not change, like a date
(year, month, day), coordinates (x, y), or a fixed list of options.

Simple Example:
python
Copy code
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana

# Trying to change an item:


fruits[1] = "orange" # Error! Tuples cannot be changed.

In short: Tuples are lists that cannot be changed.

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"]


my_list.append(4) # Add an element
print(my_list)
to add and remove

my_list = [1, 2, 3]

Action Method/Operation Description Example


Adds an item to the my_list.append(4) → [1,
Add append(item)
end of the list. 2, 3, 4]
insert(index, Inserts an item at a my_list.insert(1, 10) →
item) specific index. [1, 10, 2, 3]
extend(iterable)
Adds multiple items to my_list.extend([4, 5]) →
the list. [1, 2, 3, 4, 5]
Removes the first my_list.remove(2) → [1,
Remove remove(item)
occurrence of the item. 3]
Removes an item at the
pop(index) given index (default: my_list.pop(1) → [1, 3]
last).
Removes all items
clear() my_list.clear() → []
from the list.
list[index] = Updates an item at a my_list[1] = 10 → [1, 10,
Update new_value
specific index. 3]

Here’s the syntax to replace an element in a list in Python:

python
Copy code
list[index] = new_value

For your example, to replace 1 with 9, you would use:

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.

To get the value, you use the key:

python
Copy code
print(my_dict["name"]) # Output: Python

It will print the value associated with the key "name", which is "Python".

You might also like