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

PYTHON3

The document discusses Python pass statement, strings, lists and their operations. It explains that pass is a null operation placeholder. Strings can be created using single, double or triple quotes and support indexing, slicing and other string operations. Lists are mutable sequences that can store heterogeneous data and support operations like indexing, slicing, updating, deleting elements etc.
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)
17 views

PYTHON3

The document discusses Python pass statement, strings, lists and their operations. It explains that pass is a null operation placeholder. Strings can be created using single, double or triple quotes and support indexing, slicing and other string operations. Lists are mutable sequences that can store heterogeneous data and support operations like indexing, slicing, updating, deleting elements etc.
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/ 9

Python Pass Statement

The pass statement is also known as the null statement. The Python mediator
doesn't overlook a Remark, though a pass proclamation isn't. As a result, these two
Python keywords are distinct.

We can use the pass statement as a placeholder when unsure of the code to provide.
Therefore, the pass only needs to be placed on that line. The pass might be utilized
when we wish no code to be executed. We can simply insert a pass in cases where
empty code is prohibited, such as in loops, functions, class definitions, and if-else
statements.

Syntax

Keyword:
pass
Code
# Python program to show how to use a pass statement in a for loop
'''''pass acts as a placeholder. We can fill this place later on'''
sequence = {"Python", "Pass", "Statement", "Placeholder"}
for value in sequence:
if value == "Pass":
pass # leaving an empty if block using the pass keyword
else:
print("Not reached pass keyword: ", value)

Python String
Python string is the collection of the characters surrounded by single quotes,
double quotes, or triple quotes. The computer does not understand the
characters; internally, it stores manipulated character as the combination of the 0's
and 1's.

In Python, strings can be created by enclosing the character or the sequence of


characters in the quotes. Python allows us to use single quotes, double quotes, or
triple quotes to create the string.

Syntax:

str = "Hi Python !"


Creating String in Python

#Using single quotes


str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)

#Using triple quotes


str3 = '''''Triple quotes are generally used for
represent the multiline or
docstring'''
print(str3)

Strings indexing and splitting

Like other languages, the indexing of the Python strings starts from 0. For
example, The string "HELLO" is indexed as given in the below figure.

example:
str = "HELLO"
print(str[0])
print(str[1])
print(str[2])
print(str[3])
print(str[4])
# It returns the IndexError because 6th index doesn't exist
print(str[6])

In Python, the slice operator [] is used to access the individual characters of the
string. However, we can use the : (colon) operator in Python to access the substring
from the given string.

Example.

# Given String
str = "WELCOME TO SGHITI"
# Start Oth index to end
print(str[0:])
# Starts 1th index to 4th index
print(str[1:5])
# Starts 2nd index to 3rd index
print(str[2:4])
# Starts 0th to 2nd index
print(str[:3])
#Starts 4th to 6th index
print(str[4:7])

We can do the negative slicing in the string; it starts from the rightmost character,
which is indicated as -1. The second rightmost index indicates -2, and so on.
Consider the following image.

example

str = "WELCOME TO SGHITI"


print(str[-1])
print(str[-3])
print(str[-2:])
print(str[-4:-1])
print(str[-7:-2])
# Reversing the given string
print(str[::-1])
print(str[-12])

Python List
In Python, the sequence of various data types is stored in a list. A list is a collection
of different kinds of values or items. Since Python lists are mutable, we can change
their elements after forming. The comma (,) and the square brackets [enclose the
List's items] serve as separators.

Although six Python data types can hold sequences, the List is the most common
and reliable form. A list, a type of sequence data, is used to store the collection of
data. Tuples and Strings are two similar data formats for sequences.

# a simple list
list1 = [1, 2, "Python", "Program", 15.9]
list2 = ["Amy", "Ryan", "Henry", "Emma"]

# printing the list


print(list1)
print(list2)

# printing the type of list


print(type(list1))
print(type(list2))

Characteristics of Lists
o The lists are in order.
o The list element can be accessed via the index.
o The mutable type of List is
o The rundowns are changeable sorts.
o The number of various elements can be stored in a list.

List Indexing and Splitting


The indexing procedure is carried out similarly to string processing. The slice
operator [] can be used to get to the List's components.

list = [1,2,3,4,5,6,7]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
# Slicing the elements
print(list[0:6])
# By default, the index value is 0 so its starts from the 0th element and go for in
dex -1.
print(list[:])
print(list[2:5])
print(list[1:6:2])

# negative indexing example


list = [1,2,3,4,5]
print(list[-1])
print(list[-3:])
print(list[:-1])
print(list[-3:-1])

Updating List Values

Due to their mutability and the slice and assignment operator's ability to update
their values, lists are Python's most adaptable data structure. Python's append()
and insert() methods can also add values to a list.

APNE AAP KO EK BAAR FIR PAGAL PROVE MAT KARNA JO KAHA VO LIKHNA
JO EXAMPLE BATYA HAI VAHI LIKHNA

Delete List Values


The list elements can also be deleted by using the del keyword. Python also
provides us the remove() method if we do not know which element is to be
deleted from the list.

APNE AAP KO EK BAAR FIR PAGAL PROVE MAT KARNA JO KAHA VO LIKHNA
JO EXAMPLE BATYA HAI VAHI LIKHNA

Python List Operations


1. Concatenation

# concatenation of two lists


# declaring the lists
list1 = [12, 14, 16, 18, 20]
list2 = [9, 10, 32, 54, 86]
# concatenation operator +
l = list1 + list2
print(l)

3. Length

# size of the list


# declaring the list
list1 = [12, 14, 16, 18, 20, 23, 27, 39, 40]
# finding length of the list
len(list1)

4. Iteration

# iteration of the list


# declaring the list
list1 = [12, 14, 16, 39, 40]
# iterating
for i in list1:
print(i)
5. Membership

# membership of the list


# declaring the list
list1 = [100, 200, 300, 400, 500]
# true will be printed if value exists
# and false if not

print(600 in list1)
print(700 in list1)
print(1040 in list1)

print(300 in list1)
print(100 in list1)
print(500 in list1)

Removing Elements from the List

list = [0,1,2,3,4]
print("printing original list: ");
for i in list:
print(i,end=" ")
list.remove(2)
print("\nprinting the list after the removal of first element...")
for i in list:
print(i,end=" ")

len( )

# size of the list


# declaring the list
list1 = [12, 16, 18, 20, 39, 40]
# finding length of the list
len(list1)

Max( )
# maximum of the list
list1 = [103, 675, 321, 782, 200]
# large element in the list
print(max(list1))

Min( )

# minimum of the list


list1 = [103, 675, 321, 782, 200]
# smallest element in the list
print(min(list1))

You might also like