Python Programming – Tuple

Python Programming – Tuple

You can learn about Tuples in Python Programs with Outputs helped you to understand the language better. Python Programming – Tuple Tuple A tuple is a compound data type in Python. It is a sequence of multiple values in an ordered sequence. Tuples are very similar to lists, but tuples cannot be changed. In other words, the tuple can be viewed as a “constant list”. While lists use square brackets[ ], tuples are written with standard parentheses( ) or no parentheses, and elements are separated with comma as given below: # elements written without parentheses >>> t1 = ‘a’, ‘b’, ‘c’, ‘d’, ‘ e’# elements written within parentheses >>> t2 = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)# tuple of numbers >>> tuplel = (0, 1, 2, 3)# tuple of mixed numbers >>> tuple2 = (33, 3.3, 3+3j)# tuple of mixed data types >>> Tl = (101, “Aman”, 77]) # tuple of srtings >>> T2 = (“Apple”, “Banana”, “Orange”) # a tuple containing a single element or item >>> t3 = ‘a’, >>> type(t3) <type ‘class’> # An empty tuple >>> empty_tup = ( ) ( ) a. Syntactically, the tuple is a comma-separated (e) list of values. For example, >>> tl =…

Python Programming - List Manipulation

Python Programming – List Manipulation

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – List Manipulation List The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth. A list is a sequence of multiple values in an ordered sequence. It is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Each element or value that is inside of a list is called an item. Like a string, a list is a sequence of values. In the string, the values are characters; in a list, they can be of any type. Lists are more general than strings. Python Programming – Creating Lists Python Programming – Sequence Data Types Python Programming – Standard Data Types a. Strings are always sequences of characters between quotes, whereas lists can contain values of any type between square brackets [ ]. Lists can contain values of mixed data types. A single list can contain numbers, strings, or other lists. b. Unlike…

Python Programming - Linear Search On List

Python Programming – Linear Search On List

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – Linear Search On List Locating a particular data item in a list is known as Searching. The search application occurs in a computer when a particular data item is required from a set of stored data. The search is said to be successful if the desired data is located otherwise the search is considered to be unsuccessful. Linear or Sequential searching is finding or searching an element in a linear i.e. one after the other or traversing each and every element and checking if it is the one required. In a sequential search, each element of a list is compared one by one with the given ITEM to be searched, until either the desired element is found or the end of the list is reached. Python Programming – Accessing Lists Python Programming – Creating Lists Python Programming – The For Statement Let us take an example to understand linear search in a better way. Consider the unsorted list shown in Figure 7.14 which contains n elements. Suppose the element that is to be searched is 9. The concept of sequential search…

Python Programming – List Functions And Methods

Python Programming – List Functions And Methods

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – List Functions And Methods Python provides various methods that operate on lists. For example, append ( ) adds a new element to the end of a list, extend ( ) takes a list as an argument and appends all of the elements, sort( ) arranges the elements of the list from low to high, and pop( ) modifies the list and returns the element that was removed. Every list object that you create in Python is actually an instance of the List class. The syntax of using it is discussed below: <listObject>.<method name>( ) Python Programming – Creating Lists Python Programming – Accessing Lists Python Programming – String Functions and Methods len( ) The built-in len( ) function is used to get the length of list. >>> 1st = [ ‘ a ‘ , ‘ b ‘ , ‘ c ‘ , ‘ d ‘ ] >>> len ( a ) 4 It is possible to nest lists (create list containing other lists), for example: >>> q = [ 2 , 3 ] >>> p = [ 1 , q ,…

Python Programming - List Operations

Python Programming – List Operations

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – List Operations There are several operations that are applicable to lists. These are discussed in the following sections: Joining Lists Joining list means to concatenate (add) two lists together. The plus (+) operator, applied to two lists, produces a new list that is a combination of them: >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print(c) [1, 2, 3, 4, 5, 6] The + operator when used with lists requires both the operands to be of list type, other wise it will produce error. For example, >>> li = [10, 20 ,30] >>> li + 2 TypeError: can only concatenate list (not “int”) to list >>> li + “xyz” TypeError: can only concatenate list (not “str”) to list Python Programming – Standard Data Types Python Programming – Logical and Physical Line Python Programming – Membership Operators Repeating Lists Multiplying a list by an integer n creates a new list that repeats the original list n times. The * operator repeats a list the given number of times. >>> [0] *…

Python Programming - Accessing Lists

Python Programming – Accessing Lists

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – Accessing Lists Lists are container data type that acts as a dynamic array. In other words, the list is a sequence that can be indexed into and can grow and shrink. List elements have index numbers. Indexing and slicing operations on lists mostly work in the same way as they do with strings. List indexing is zero-based as it is with strings. Working from left to right, the first element has the index 0, the next has the index 1, and so on. Num = [ 45 , 67 , 56 , 34 ] print( Num ( 0 ) ) it prints 45 because the location of a list starts at position 0. Python Programming – String Slices Python Programming – Logical and Physical Line Python Programming – Standard Data Types Indexing The list data type is a container that holds a number of elements in a given order. For accessing an element of the list, indexing is used. Each item in a list corresponds to an index number, which is an integer value, starting with the index number 0 goes…

Python Programming – Creating Lists

Python Programming – Creating Lists

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better. Python Programming – Creating Lists There are several ways to create a new list; the simplest way is to enclose the elements in square brackets [ ], separated by commas. It can have any number of items and they may be of different types (integer, float, string, etc.). The syntax for creating a list is: <list_name>          =         [<value>, <value>, <value>] For example, you can create a list of cities having three items such as Delhi, Mumbai, and Chennai, separated by a comma and enclosed inside square brackets. City=[ ‘ Delhi ‘ ,  ‘ Mumbai ‘ ,  ‘ Chennai ‘ ] Python Programming – Standard Data Types Python Programming – Logical and Physical Line Python Programming – String Functions and Methods Other examples of Python lists are: >>> L1 = [ 10 , 20 , 30 , 40 ] # list of 4 integer elements >>> L2 = [ ” Raj ” ,  ” Govind ” ,  ” Abdul ” ] # list of 3 strings >>> L3 = [ 1 ,  1 . 5 ,  2 .…

Python Programming - Sequence Data Types

Python Programming – Sequence Data Types

You can learn about Strings in Python Programs with Outputs helped you to understand the language better. Python Programming – Sequence Data Types A sequence is an ordered collection of items, indexed by positive integers. It is a combination of mutable (a mutable variable is one, whose value may change) and immutable (an immutable variable is one, whose value may not change) data types. There are three types of sequences available in Python; they are Strings, Lists, and Tuples. They share much of the same syntax and functionality. The string is an ordered sequence of letters/characters. For example, your name can be considered a string. Strings are defined using quotes – single quotes (”), double quotes (“”) or triple quotes (“””). The list is also a sequence of values of any type. Python Programming – Standard Data Types Python Programming – Variable Python Programming – String Lists are defined using square brackets and commas. Tuples are a sequence of values of any type and are indexed by integers. They are defined using parentheses (and commas). You can access individual members of a string, list, or tuple using square bracket “array” notation. We will cover Sequence Data Types – List and Tuple…

Python Programming – String Functions and Methods

Python Programming – String Functions and Methods

You can learn about Strings in Python Programs with Outputs helped you to understand the language better. Python Programming – String Functions and Methods Python provides built-in functions and methods that allow us to do string manipulation. Every string object that you create in python is actually an instance of string calls. The string manipulation methods that are discussed below use the following syntax: <stringobject>.<method name>( ) len ( ) len() is a built-in function that returns the number of characters in a string. >>> fruit = ‘ apple ‘ >>> len ( fruit ) 5 An empty string contains no chapters and has length O, but other than that, it is the same as any other string. A string with a length of 1 represents a chapter in python. a = [‘apple’, ‘banana’, ‘pomegranate’] for x in a: print(x, len(x)) str1 = ‘Hello World’ len(str1) Str1 = ‘Apple is healthy. Apple is also good for health.’ print () print(strl.count(“Apple”)) print () str = “elephant” count = 0 for i in str: count = count+1 print(count) Python Programming – Logical and Physical Line Python Programming – Standard Data Types Python Programming – Special String Operators capitalize( ) The method capitalize() returns a…

Python Programming – String Slices

Python Programming – String Slices

You can learn about Strings in Python Programs with Outputs helped you to understand the language better. Python Programming – String Slices Slicing is a mechanism used to select a range of items from the sequence. You can retrieve a slice (sub-string) of a string with a slice operation. The slicing operation is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon enclosed within [ ] (square brackets). The slice operator [ ] is used to access the individual characters of the string. However, you can use the : (colon) operator in Python to access the substring. Note that this is very similar to the indexing operation you have been using till now. Characters are accessible using their position in the string which has to be enclosed in brackets following the string. The position number can be positive or negative, which means starting at the beginning of the end of the string. Substrings are extracted by providing values of the start and end positions separated by a colon. Both positions are optional, which means you can choose to start extracting at the beginning of the string or to extract the substring until…