Unit 1 Python
Unit 1 Python
Unit 1 Python
In Python, there are several built-in data types that allow you to store and manipulate different
kinds of data. Here are some commonly used data types in Python:
1. Numeric Types:
```python
x = 10
y = -5
```
```python
pi = 3.14
height = 1.85
```
```python
z = 2 + 3j
```
```python
```
```python
is_active = True
is_happy = False
```
4. List (`list`): Represents an ordered collection of elements, which can be of different types. Lists are
mutable.
```python
```
5. Tuple (`tuple`): Similar to lists, but tuples are immutable (cannot be modified after creation).
```python
```
```python
```
7. Dictionary (`dict`): Represents a collection of key-value pairs, where each key is unique.
Dictionaries are mutable.
```python
```
```python
result = None
```
These are some of the fundamental data types in Python. Each data type has its own properties and
methods that allow you to perform specific operations on the data.
2.write about standard input and output function in python with examples
In Python, you can interact with the user and display information using standard input and output
functions. The two main functions for this purpose are `input()` and `print()`. Here's an explanation of
each function with examples:
1. `input()`: This function allows you to prompt the user for input and read it from the console. It
returns a string containing the user's input.
```python
```
In the example above, the user is prompted to enter their name, and the input is stored in the
`name` variable. The `print()` function is then used to display a greeting message along with the
user's name.
2. `print()`: This function is used to display information or output values to the console. It can accept
multiple arguments separated by commas, which will be concatenated and displayed.
```python
x = 10
y=5
```
In this example, the `print()` function is used to display the values of variables `x` and `y`. The
output will be: "The value of x is 10 and the value of y is 5."
You can also use formatting options to control how the output is displayed. Here's an example using
f-strings:
```python
name = "Alice"
age = 25
```
The `f` before the string allows you to include variables directly within curly braces `{}`. The output
will be: "My name is Alice and I am 25 years old."
These standard input and output functions are essential for interacting with the user and providing
information or feedback in your Python programs. They enable you to create interactive and dynamic
applications.
3. describe basic operators of lists .give examples
In Python, lists have several basic operators that allow you to manipulate and perform operations on
them. Here are some of the commonly used operators for lists, along with examples:
1. Concatenation (`+`):
The `+` operator can be used to concatenate two lists, creating a new list that contains all the
elements of both lists.
```python
list1 = [1, 2, 3]
list2 = [4, 5, 6]
```
2. Repetition (`*`):
The `*` operator can be used to repeat a list a certain number of times, creating a new list.
```python
list1 = [1, 2, 3]
repeated_list = list1 * 3
```
The `in` and `not in` operators are used to check if an element is present in a list.
```python
list1 = [1, 2, 3, 4, 5]
```
4. Indexing (`[]`):
Lists can be accessed using indexing. Indexing starts from 0 for the first element in the list.
```python
print(list1[0]) # Output: 10
print(list1[3]) # Output: 40
```
5. Slicing (`[:]`):
Slicing allows you to extract a portion of a list by specifying start and end indices.
```python
list1 = [1, 2, 3, 4, 5]
sliced_list = list1[1:4]
```
6. Length (`len()`):
```python
list1 = [1, 2, 3, 4, 5]
length = len(list1)
print(length) # Output: 5
```
7. Assignment (`=`):
list1 = [1, 2, 3, 4, 5]
list1[2] = 10
```
These basic operators allow you to perform various operations on lists, such as combining, repeating,
accessing elements, checking membership, and modifying values. They provide flexibility in
manipulating and working with lists in Python.
In Python, tuples are similar to lists but are immutable, meaning their elements cannot be modified
after creation. Here are some basic operators commonly used with tuples, along with examples:
1. Indexing (`[]`):
```python
print(tuple1[0]) # Output: 10
print(tuple1[3]) # Output: 40
```
2. Slicing (`[:]`):
Slicing allows you to extract a portion of a tuple by specifying start and end indices.
```python
tuple1 = (1, 2, 3, 4, 5)
sliced_tuple = tuple1[1:4]
```
3. Length (`len()`):
The `len()` function returns the number of elements in a tuple.
```python
tuple1 = (1, 2, 3, 4, 5)
length = len(tuple1)
print(length) # Output: 5
```
4. Concatenation (`+`):
The `+` operator can be used to concatenate two tuples, creating a new tuple that contains all the
elements of both tuples.
```python
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
```
5. Repetition (`*`):
The `*` operator can be used to repeat a tuple a certain number of times, creating a new tuple.
```python
tuple1 = (1, 2, 3)
repeated_tuple = tuple1 * 3
```
The `in` and `not in` operators are used to check if an element is present in a tuple.
```python
tuple1 = (1, 2, 3, 4, 5)
```
These basic operators allow you to perform operations on tuples, such as accessing elements, slicing,
checking membership, concatenating, and repeating. Tuples are commonly used when you have a
collection of values that should not be modified, such as coordinates, configuration settings, or
database records.
In Python, dictionaries are data structures that store a collection of key-value pairs. They are
commonly used for mapping and associating values with unique keys. Here are some basic operators
commonly used with dictionaries, along with examples:
You can access the value associated with a specific key in a dictionary using square brackets.
```python
print(student['age']) # Output: 20
```
You can update the value associated with a key by assigning a new value to it using square brackets.
```python
student['grade'] = 'B'
```
3. Adding Key-Value Pairs (`[]`):
You can add new key-value pairs to a dictionary by assigning a value to a new key using square
brackets.
```python
student['grade'] = 'A'
```
You can delete a key-value pair from a dictionary using the `del` keyword.
```python
del student['age']
```
The `in` and `not in` operators can be used to check if a key exists in a dictionary.
```python
```
6. Length (`len()`):
print(len(student)) # Output: 3
```
These basic operators allow you to manipulate dictionaries in Python, including accessing values,
updating values, adding and deleting key-value pairs, checking membership, and getting the length of
a dictionary. Dictionaries are useful when you need to associate values with unique identifiers or
keys.
Certainly! Here's an explanation of sets and string operations in Python, along with examples:
1. Sets:
In Python, a set is an unordered collection of unique elements. Sets are useful for operations like
membership testing and removing duplicates. Here are some common set operations:
- Creating a set:
```python
set1 = {1, 2, 3, 4, 5}
```
```python
set1.add(6)
```
```python
set1.remove(2)
```
- Union of sets:
```python
set3 = set1.union(set2)
```
- Intersection of sets:
```python
set4 = set1.intersection(set2)
```
```python
set5 = set1.difference(set2)
```
- Checking membership:
```python
```
2. String Operations:
Strings are sequences of characters in Python. They support various operations that allow you to
manipulate and analyze text data. Here are some common string operations:
- Concatenating strings:
```python
str1 = "Hello"
str2 = "World"
```
- Accessing characters using indexing:
```python
str1 = "Hello"
```
- Slicing strings:
```python
sliced_str = str1[7:12]
```
```python
str1 = "Hello"
length = len(str1)
print(length) # Output: 5
```
- Converting cases:
```python
```
```python
```
- Splitting strings:
```python
```
- Replacing characters:
```python
```
These are some of the basic operations you can perform on sets and strings in Python.
Understanding these operations allows you to manipulate and work with sets and strings effectively
in your Python programs.