Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views6 pages

Python Manual - Session 2 (Bootcamp)

Uploaded by

laibaabbasi6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Python Manual - Session 2 (Bootcamp)

Uploaded by

laibaabbasi6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Basics Manual- Session 2

Objective
The objective of this session is to provide a comprehensive overview of Python basics, focusing
on data types. By the end of this session, learners will have a solid understanding of these
fundamental aspects of Python, enabling them to write more effective and efficient code.

Table of Contents
1. Exploring Sequence Data Types
o Strings
o Lists
o Tuple
o Sets
o Dictionary

Sequence Data Type in Python


The sequence Data Type in Python is the ordered collection of similar or different data
types. Sequences allow storing of multiple values in an organized and efficient way.
There are five data types in the Python programming language:

• String: A string is a collection of one or more characters put in a single, double, or triple
quote. In python there is no character data type, a character is a string of length one.

• List: List is a collection which is ordered and changeable. Allows duplicate members.

• Tuple: Tuple is a collection which is ordered and unchangeable. Allows duplicate


members.

• Set: Set is a collection which is unordered, unchangeable, and unindexed. No duplicate


members.

• Dictionary: Dictionary is a collection which is ordered and changeable. No duplicate


members.
1. String Data Type
A string is a collection of one or more characters put in a single quote, double-quote, or
triple-quote. In python there is no character data type, a character is a string of length one. It is
represented by str class.

Strings Manipulation

Strings are sequences of characters and are one of the most commonly used data types in Python.

Creating a String

• Usage: Define a string using single, double, or triple quotes.


• Example:

my_string = "Hello, World!"


another_string = 'Hello, Python!'
multiline_string = """This is a
multiline string."""

String Methods:
Upper and Lower Case

• Usage: Converts a string to uppercase or lowercase.


• Example:

upper_string = my_string.upper() # result is "HELLO, WORLD!"


lower_string = my_string.lower() # result is "hello, world!"

Strip

• Usage: Removes whitespace from the beginning and end of a string.


• Example:

stripped_string = " Hello, World! ".strip() # result is "Hello,


World!"

Split

• Usage: Splits a string into a list of substrings based on a specified delimiter.


• Example:

split_string = my_string.split(",") # result is ['Hello', ' World!']


Join

• Usage: Joins elements of a list into a single string, separated by a specified delimiter.
• Example:

joined_string = "-".join(["Hello", "World"]) # result is "Hello-World"

Find

• Usage: Returns the index of the first occurrence of a substring.


• Example:

index = my_string.find("World") # result is 7

Replace

• Usage: Replaces a substring with another substring.


• Example:

replaced_string = my_string.replace("World", "Python") # result is


"Hello, Python!"

By mastering these fundamental concepts, learners will be well-prepared to tackle more


advanced Python programming challenges.

2. List

A list is an ordered, mutable collection of items. Lists are just like arrays, which is an ordered
collection of data. It is very flexible as the items in a list do not need to be of the same type. Lists
are defined using square brackets [].

Examples:

my_list = [1, 2, 3, 4, 5]
print(my_list[0]) #1
my_list.append(6) # Adds 6 to the end of the list
print(my_list) # [1, 2, 3, 4, 5, 6]
my_list[2] = 10 # Changes the third element to 10
print(my_list) # [1, 2, 10, 4, 5, 6]
Python has a set of built-in methods that you can use on lists/arrays.

3. Tuples
Tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception
that tuples cannot be changed once declared. Tuples are usually faster than lists. Tuples are
defined using parentheses ().

Examples:

my_tuple = (1, 2, 3, 4, 5)

print(my_tuple[0]) #1

# my_tuple[2] = 10 # This would raise an error since tuples are immutable

print(my_tuple) # (1, 2, 3, 4, 5)
• Tuples are Ordered:
When we say that tuples are ordered, it means that the items have a defined order, and
that order will not change.

• Tuples are Unchangeable:


Tuples are unchangeable, meaning that we cannot change, add or remove items after
the tuple has been created.

Note: To create a tuple with only one item, you have to add a comma after the item, otherwise
Python will not recognize it as a tuple.

4. Sets

Sets are unordered collections of unique elements. They are useful for performing mathematical
set operations like union, intersection, difference, and symmetric difference.

Creating a Set

• Usage: Define a set using curly braces {} or the set() function.


• Example:

my_set = {1, 2, 3, 4, 5}

another_set = set([1, 2, 3, 4, 5])

Set Operations

Union (|)

• Usage: Combines elements from both sets, removing duplicates.


• Example:set1 = {1, 2, 3}

set2 = {3, 4, 5}
result = set1 | set2 # result is {1, 2, 3, 4, 5}

Intersection (&)

• Usage: Returns elements present in both sets.


• Example:

result = set1 & set2 # result is {3}


Difference (-)

• Usage: Returns elements present in the first set but not in the second set.
• Example:

result = set1 - set2 # result is {1, 2}

Symmetric Difference (^)

• Usage: Returns elements present in either set but not in both.


• Example:

result = set1 ^ set2 # result is {1, 2, 4, 5}

5. Dictionaries
A dictionary is an unordered collection of key-value pairs. Dictionaries are defined using curly
braces {}. Dictionary holds pairs of values, one being the Key and the other corresponding pair
element being its Key:value. Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated.

Examples:

my_dict = {'a': 1, 'b': 2, 'c': 3}


print(my_dict['a']) # 1
my_dict['d'] = 4 # Adds a new key-value pair 'd': 4
print(my_dict) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}
del my_dict['b'] # Deletes the key 'b' and its value
print(my_dict) # {'a': 1, 'c': 3, 'd': 4}

You might also like