Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Unit 3

The document covers Python's complex data types, including strings, lists, tuples, and dictionaries, along with their operations and manipulations. It explains string operations such as concatenation, repetition, indexing, and slicing, as well as list slicing and dictionary manipulation methods. Additionally, it discusses the importance of functions in organizing and reusing code in Python programming.

Uploaded by

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

Python Unit 3

The document covers Python's complex data types, including strings, lists, tuples, and dictionaries, along with their operations and manipulations. It explains string operations such as concatenation, repetition, indexing, and slicing, as well as list slicing and dictionary manipulation methods. Additionally, it discusses the importance of functions in organizing and reusing code in Python programming.

Uploaded by

mitv25012024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT III

Python Complex data types: Using string data type and string operations, Defining list

and list slicing, Use of Tuple data type. String, List and Dictionary, Manipulations Building

blocks of python programs, string manipulation methods, List manipulation. Dictionary

manipulation, Programming using string, list and dictionary in-built functions. Python

Functions, Organizing python codes using functions..


1. String Data Type and String Operations:

Definition:
A string is a sequence of characters enclosed within single (' '), double (" "), or triple (''' ''' or """ """) quotes
in Python.

String Operations:
Concatenation (+): Combines two strings.

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2 # Concatenation
print(result) # Output: Hello World

29-10-2020 Side 2
• Repetition (*): Repeats a string a specified number of
times.python

str_repeat = "abc" * 3 # Repetition


print(str_repeat) # Output: abcabcabc

• Indexing ([]): Access individual characters using their


position.

first_char = result[0] # Indexing


print(first_char) # Output: H

• Slicing ([start:stop:step]): Extracts a substring from the


original string.
substring = result[0:5] # Slicing
print(substring) # Output: Hello
• Length (len()): Returns the number of characters in a string.

length = len(result) # Length


print(length) # Output: 11

• String Methods: Functions that perform various operations on strings (e.g., lower(),
upper(), replace()).

lowercase = result.lower() # String Method


print(lowercase) # Output: hello world
2. List and List Slicing:
• Definition: A list is a mutable, ordered sequence of elements in Python,
represented by square brackets [].
• List Slicing:
Allows extracting a portion of a list using list[start:stop:step].

my_list = [1, 2, 3, 4, 5]
sublist = my_list[1:4] # List Slicing
print(sublist) # Output: [2, 3, 4]
3. Tuple Data Type:

Definition: A tuple is an immutable, ordered collection of elements in Python,


represented by parentheses ().

Use: Suitable for representing fixed collections of values that should not be modified
during program execution.

my_tuple = (1, 2, 3)
4. String, List, and Dictionary Manipulations:

• Building Blocks of Python Programs:

> Strings, lists, and dictionaries are fundamental data types.


> Used for storing and manipulating data.

• String Manipulation Methods:

> Methods like split(), join(), strip(), etc., facilitate string manipulation.

sentence = "This is a sample sentence.”


words = sentence.split() # String Method
print(words) # Output: ['This', 'is', 'a', 'sample', 'sentence.’]

• List Manipulation:
Methods like append(), remove(), extend(), etc., modify lists.

numbers = [1, 2, 3]
numbers.append(4) # List Method
print(numbers) # Output: [1, 2, 3, 4]
Dictionary Manipulation:

• Key-value pairs provide a flexible data structure.


• Methods like keys(), values(), items(), etc., offer ways to manipulate dictionaries.

my_dict = {'name': 'John', 'age': 25}


keys_list = list(my_dict.keys()) # Dictionary Method
print(keys_list) # Output: ['name', 'age']
5. Programming Using String, List, and Dictionary In-built Functions:
Python Functions:
>Encapsulate reusable code.
>Defined using the def keyword.
>May have parameters and return values.
#Code
def greet(name):
return f"Hello, {name}!"
message = greet("Alice") # Function
print(message) # Output: Hello, Alice!
Organizing Python Codes Using Functions:
>Functions enhance code modularity and reusability.
>Grouping related functionality into functions improves code readability.
#Code
def calculate_sum(numbers):
return sum(numbers)
result = calculate_sum([1, 2, 3, 4, 5]) # Function
print(result) # Output: 15

You might also like