Unit 1 Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

1.

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:

- Integer (`int`): Represents whole numbers, positive or negative.

```python

x = 10

y = -5

```

- Float (`float`): Represents decimal numbers.

```python

pi = 3.14

height = 1.85

```

- Complex (`complex`): Represents numbers with a real and imaginary part.

```python

z = 2 + 3j

```

2. String (`str`): Represents a sequence of characters enclosed in single or double quotes.

```python

name = "John Doe"

message = 'Hello, world!'

```

3. Boolean (`bool`): Represents either True or False.

```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

fruits = ['apple', 'banana', 'cherry']


numbers = [1, 2, 3, 4, 5]

```

5. Tuple (`tuple`): Similar to lists, but tuples are immutable (cannot be modified after creation).

```python

coordinates = (2.5, 4.8)

rgb_values = (255, 128, 0)

```

6. Set (`set`): Represents an unordered collection of unique elements.

```python

colors = {'red', 'green', 'blue'}

```

7. Dictionary (`dict`): Represents a collection of key-value pairs, where each key is unique.
Dictionaries are mutable.

```python

person = {'name': 'Alice', 'age': 25, 'city': 'New York'}

```

8. NoneType (`None`): Represents the absence of a value or a null value.

```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

name = input("Enter your name: ")


print("Hello,", name)

```

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

print("The value of x is", x, "and the value of y is", y)

```

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

print(f"My name is {name} and I am {age} years old.")

```

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]

concatenated_list = list1 + list2

print(concatenated_list) # Output: [1, 2, 3, 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

print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

```

3. Membership (`in` and `not in`):

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]

print(3 in list1) # Output: True

print(6 not in list1) # Output: True

```
4. Indexing (`[]`):

Lists can be accessed using indexing. Indexing starts from 0 for the first element in the list.

```python

list1 = [10, 20, 30, 40, 50]

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]

print(sliced_list) # Output: [2, 3, 4]

```

6. Length (`len()`):

The `len()` function returns the number of elements in a list.

```python

list1 = [1, 2, 3, 4, 5]

length = len(list1)

print(length) # Output: 5

```

7. Assignment (`=`):

You can assign a new value to a specific index in a list.


```python

list1 = [1, 2, 3, 4, 5]

list1[2] = 10

print(list1) # Output: [1, 2, 10, 4, 5]

```

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.

4. discribe basic operators of tuples in python .give examples.

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 (`[]`):

Tuples can be accessed using indexing, just like lists.

```python

tuple1 = (10, 20, 30, 40, 50)

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]

print(sliced_tuple) # Output: (2, 3, 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)

concatenated_tuple = tuple1 + tuple2

print(concatenated_tuple) # Output: (1, 2, 3, 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

print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

```

6. Membership (`in` and `not in`):

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)

print(3 in tuple1) # Output: True

print(6 not in tuple1) # Output: True

```

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.

5. write about basic operators in dictionaries in python.give examples.

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:

1. Accessing Values (`[]`):

You can access the value associated with a specific key in a dictionary using square brackets.

```python

student = {'name': 'Alice', 'age': 20, 'grade': 'A'}

print(student['name']) # Output: 'Alice'

print(student['age']) # Output: 20

```

2. Updating Values (`[]`):

You can update the value associated with a key by assigning a new value to it using square brackets.

```python

student = {'name': 'Alice', 'age': 20, 'grade': 'A'}

student['grade'] = 'B'

print(student) # Output: {'name': 'Alice', 'age': 20, '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 = {'name': 'Alice', 'age': 20}

student['grade'] = 'A'

print(student) # Output: {'name': 'Alice', 'age': 20, 'grade': 'A'}

```

4. Deleting Key-Value Pairs (`del`):

You can delete a key-value pair from a dictionary using the `del` keyword.

```python

student = {'name': 'Alice', 'age': 20, 'grade': 'A'}

del student['age']

print(student) # Output: {'name': 'Alice', 'grade': 'A'}

```

5. Membership (`in` and `not in`):

The `in` and `not in` operators can be used to check if a key exists in a dictionary.

```python

student = {'name': 'Alice', 'age': 20, 'grade': 'A'}

print('age' in student) # Output: True

print('score' not in student) # Output: True

```

6. Length (`len()`):

The `len()` function returns the number of key-value pairs in a dictionary.


```python

student = {'name': 'Alice', 'age': 20, 'grade': 'A'}

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.

6. write about sets and string operations in python . give examples.

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}

set2 = set([4, 5, 6, 7])

```

- Adding elements to a set:

```python

set1.add(6)

```

- Removing elements from a set:

```python

set1.remove(2)

```
- Union of sets:

```python

set3 = set1.union(set2)

```

- Intersection of sets:

```python

set4 = set1.intersection(set2)

```

- Difference between sets:

```python

set5 = set1.difference(set2)

```

- Checking membership:

```python

print(3 in set1) # Output: True

```

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"

result = str1 + " " + str2

print(result) # Output: "Hello World"

```
- Accessing characters using indexing:

```python

str1 = "Hello"

print(str1[0]) # Output: "H"

```

- Slicing strings:

```python

str1 = "Hello, World!"

sliced_str = str1[7:12]

print(sliced_str) # Output: "World"

```

- Getting the length of a string:

```python

str1 = "Hello"

length = len(str1)

print(length) # Output: 5

```

- Converting cases:

```python

str1 = "Hello, World!"

print(str1.lower()) # Output: "hello, world!"

print(str1.upper()) # Output: "HELLO, WORLD!"

```

- Checking for substrings:

```python

str1 = "Hello, World!"


print("Hello" in str1) # Output: True

```

- Splitting strings:

```python

str1 = "Hello, World!"

words = str1.split(", ")

print(words) # Output: ["Hello", "World!"]

```

- Replacing characters:

```python

str1 = "Hello, World!"

new_str = str1.replace("World", "Python")

print(new_str) # Output: "Hello, 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy