KEMBAR78
ppt notes python language operators and data | PDF
Basics of
Python
STRINGS AND LISTS
String
 A String is a data structure in Python that
represents a sequence of characters. It is an
immutable data type, meaning that once you
have created a string, you cannot change it.
Strings are used widely in many different
applications, such as storing and manipulating
text data, representing names, addresses, and
other types of data that can be represented as
text.
 Example:
 "Geeksforgeeks" or 'Geeksforgeeks' or "a"
Creating a String in Python
 Strings in Python can be created using single
quotes or double quotes or even triple quotes. Let
us see how we can define a string in Python.
 Example:
 In this example, we will demonstrate different
ways to create a Python String. We will create a
string using single quotes (‘ ‘), double quotes (” “),
and triple double quotes (“”” “””). The triple
quotes can be used to declare multiline strings in
Python.
Accessing characters in
Python String
 In Python, individual characters of a String
can be accessed by using the method of
Indexing. Indexing allows negative address
references to access characters from the back of
the String, e.g. -1 refers to the last character, -2
refers to the second last character, and so on.
 While accessing an index out of the range
will cause an IndexError. Only Integers are allowed
to be passed as an index, float or other types that
will cause a TypeError.
Example
 In this example, we will define a string in
Python and access its characters using positive
and negative indexing. The 0th element will be
the first character of the string whereas the -1th
element is the last character of the string.
String Slicing
 In Python, the String Slicing method is used to
access a range of characters in the String. Slicing
in a String is done by using a Slicing operator, i.e.,
a colon (:). One thing to keep in mind while using
this method is that the string returned after slicing
includes the character at the start index but not
the character at the last index.
Reversing a Python String
 By accessing characters from a string, we
can also reverse strings in Python. We can Reverse
a string by using String slicing method.
Updating Entire String
 As Python strings are immutable in nature, we
cannot update the existing string. We can only
assign a completely new value to the variable
with the same name.
Formatting of Strings
 Strings in Python can be formatted with the
use of format() method which is a very versatile
and powerful tool for formatting Strings. Format
method in String contains curly braces {} as
placeholders which can hold arguments
according to position or keyword to specify the
order.
 # Python Program for
 # Formatting of Strings
 # Default order
 String1 = "{} {} {}".format('Geeks', 'For', 'Life')
 print("Print String in default order: ")
 print(String1)
 # Positional Formatting
 String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')
 print("nPrint String in Positional order: ")
 print(String1)
 # Keyword Formatting
 String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')
 print("nPrint String in order of Keywords: ")
 print(String1)
isupper() in Python
 In Python, isupper() is a built-in method used for
string handling. This method returns “True” if all
characters in the string are uppercase, otherwise,
returns “False”.
 It returns “True” for whitespaces but if there is only
whitespace in the string then returns “False”.
 It does not take any arguments, Therefore,
 It returns an error if a parameter is passed.
 Digits and symbols return “True” but if the
 string contains only digits and numbers
 then returns “False”
 This function is used to check if the argument
contains any uppercase characters such as:
islower() in Python
 In Python, islower() is a built-in method used
for string handling. The islower() method
returns True if all characters in the string are
lowercase, otherwise, returns “False”.
 It returns “True” for whitespaces but if there is only
whitespace in the string then returns “False”.
 It does not take any arguments, Therefore, It
returns an error if a parameter is passed.
 Digits and symbols return “True” but if the string
contains only digits and numbers then returns
“False”.
 This function is used to check if the argument
contains any lowercase characters such as :
upper() in Python
 In Python, upper() is a built-in method used
for string handling. The upper() method returns the
uppercased string from the given string. It
converts all lowercase characters to uppercase. If
no lowercase characters exist, it returns the
original string.
 It does not take any arguments, Therefore, It
returns an error if a parameter is passed. Digits
and symbols return are returned as it is, Only a
lowercase letter is returned after converting to
uppercase.
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.
 Lists written in Python are identical to dynamically
scaled arrays defined in other languages, such as
Array List in Java and Vector in C++. A list is a
collection of items separated by commas and
denoted by the symbol [].
List Declaration
Characteristics of Lists
 The lists are in order.
 The list element can be accessed via the index.
 The mutable type of List is
 The rundowns are changeable sorts.
 The number of various elements can be stored in
a list.
Python List Operations
 In Python, List is a sequence. Hence, we can
concatenate two lists with "+" operator and
concatenate multiple copies of a list with "*"
operator. The membership operators "in" and "not
in" work with list object.
List methods in Python
 Python List Methods has multiple methods to
work with Python lists, Below we’ve explained all
the methods you can use with Python lists, for
example, append(), copy(), insert(), and more.
Adding Element in List
 Python append()
 Used for appending and adding elements to the
List. It is used to add elements to the last position
of the List in Python.
 Syntax: list.append (element)
Python insert()
 Inserts an element at the specified position.
 Syntax:
 list.insert(<position, element)
Python extend()
 Adds contents to List2 to the end of List1.
 Syntax: List1.extend(List2)

ppt notes python language operators and data

  • 1.
  • 2.
    String  A Stringis a data structure in Python that represents a sequence of characters. It is an immutable data type, meaning that once you have created a string, you cannot change it. Strings are used widely in many different applications, such as storing and manipulating text data, representing names, addresses, and other types of data that can be represented as text.  Example:  "Geeksforgeeks" or 'Geeksforgeeks' or "a"
  • 3.
    Creating a Stringin Python  Strings in Python can be created using single quotes or double quotes or even triple quotes. Let us see how we can define a string in Python.  Example:  In this example, we will demonstrate different ways to create a Python String. We will create a string using single quotes (‘ ‘), double quotes (” “), and triple double quotes (“”” “””). The triple quotes can be used to declare multiline strings in Python.
  • 4.
    Accessing characters in PythonString  In Python, individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, and so on.  While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types that will cause a TypeError.
  • 5.
    Example  In thisexample, we will define a string in Python and access its characters using positive and negative indexing. The 0th element will be the first character of the string whereas the -1th element is the last character of the string.
  • 6.
    String Slicing  InPython, the String Slicing method is used to access a range of characters in the String. Slicing in a String is done by using a Slicing operator, i.e., a colon (:). One thing to keep in mind while using this method is that the string returned after slicing includes the character at the start index but not the character at the last index.
  • 7.
    Reversing a PythonString  By accessing characters from a string, we can also reverse strings in Python. We can Reverse a string by using String slicing method.
  • 8.
    Updating Entire String As Python strings are immutable in nature, we cannot update the existing string. We can only assign a completely new value to the variable with the same name.
  • 9.
    Formatting of Strings Strings in Python can be formatted with the use of format() method which is a very versatile and powerful tool for formatting Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.
  • 10.
     # PythonProgram for  # Formatting of Strings  # Default order  String1 = "{} {} {}".format('Geeks', 'For', 'Life')  print("Print String in default order: ")  print(String1)  # Positional Formatting  String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')  print("nPrint String in Positional order: ")  print(String1)  # Keyword Formatting  String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')  print("nPrint String in order of Keywords: ")  print(String1)
  • 11.
    isupper() in Python In Python, isupper() is a built-in method used for string handling. This method returns “True” if all characters in the string are uppercase, otherwise, returns “False”.  It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.  It does not take any arguments, Therefore,  It returns an error if a parameter is passed.  Digits and symbols return “True” but if the  string contains only digits and numbers  then returns “False”  This function is used to check if the argument contains any uppercase characters such as:
  • 12.
    islower() in Python In Python, islower() is a built-in method used for string handling. The islower() method returns True if all characters in the string are lowercase, otherwise, returns “False”.  It returns “True” for whitespaces but if there is only whitespace in the string then returns “False”.  It does not take any arguments, Therefore, It returns an error if a parameter is passed.  Digits and symbols return “True” but if the string contains only digits and numbers then returns “False”.  This function is used to check if the argument contains any lowercase characters such as :
  • 14.
    upper() in Python In Python, upper() is a built-in method used for string handling. The upper() method returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.  It does not take any arguments, Therefore, It returns an error if a parameter is passed. Digits and symbols return are returned as it is, Only a lowercase letter is returned after converting to uppercase.
  • 15.
    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.  Lists written in Python are identical to dynamically scaled arrays defined in other languages, such as Array List in Java and Vector in C++. A list is a collection of items separated by commas and denoted by the symbol [].
  • 16.
  • 17.
    Characteristics of Lists The lists are in order.  The list element can be accessed via the index.  The mutable type of List is  The rundowns are changeable sorts.  The number of various elements can be stored in a list.
  • 18.
    Python List Operations In Python, List is a sequence. Hence, we can concatenate two lists with "+" operator and concatenate multiple copies of a list with "*" operator. The membership operators "in" and "not in" work with list object.
  • 19.
    List methods inPython  Python List Methods has multiple methods to work with Python lists, Below we’ve explained all the methods you can use with Python lists, for example, append(), copy(), insert(), and more.
  • 20.
    Adding Element inList  Python append()  Used for appending and adding elements to the List. It is used to add elements to the last position of the List in Python.  Syntax: list.append (element)
  • 21.
    Python insert()  Insertsan element at the specified position.  Syntax:  list.insert(<position, element)
  • 22.
    Python extend()  Addscontents to List2 to the end of List1.  Syntax: List1.extend(List2)