Skip to main content

Posts

Showing posts from August, 2017

Tuple in Python

Tuples are used for grouping data.A tuple is an immutable list, i.e. a tuple cannot be changed in any way once it has been created. A tuple is defined analogously to lists, except that the set of elements is enclosed in parentheses instead of square brackets. The rules for indices are the same as for lists. Once a tuple has been created, you can't add elements to a tuple or remove elements from a tuple. Where is the benefit of tuples? Tuples are faster than lists. If you know that some data doesn't have to be changed, you should use tuples instead of lists, because this protects your data against accidental changes. Tuples can be used as keys in dictionaries, while lists can't. The following example shows how to define a tuple and how to access a tuple.  Furthermore we can see that we raise an error, if we try to assign a new value to an element of a tuple: Tuple creation >>> t = ("tuples", "are", "immutable") >

List in Python

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted. List is one of the simplest and most important data structures in Python. Lists are enclosed in square brackets [ ] and each item is separated by a comma. Lists are collections of items where each item in the list has an assigned index value. A list is mutable, meaning you can change its contents. Lists are very flexible and have many built-in control functions Creating Lists To create a list, place a (possibly empty) comma-separated list of objects or expressions in square brackets. For example, the following initialize

Sequence

Sequence In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important. Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted. Tuples are like lists, but they are immutable - they can't be changed. Strings are a special type of sequence that can only store characters, and they have a special notation. However, all of the sequence operations described below can also be used on strings. Sequence Operations + combines two sequences in a process called concatenation. For example, [1,2,3]+[4,5] will evaluate to [1,2,3,4,5]. * repeats a sequence a (positive integral) number of times. For example, [1,11]*3 will evaluate to [1,11,1,11,1,11]. x in mySeq will return True if x is an element of mySeq, and False otherwise. You can negate thi

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes. Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language""" This will create a string object s1, s2 and s3 using different forms. Individual characters can be accessed using different subscripts( +ve or –ve) with the string object. Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’ We can also slice a string. Segment of a string is called a slice. >>>s[1:3] ‘yt’ If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’ >>>s[2:6] ‘thon’ If the l

Operators, Precedence and Expression Evaluation

Operators are special symbols which represents computation. They are applied on operand(s), which can be values(constants) or variables. Same operator can behave differently on different data types. Operators when applied on operands form an expression Operators are categorized as Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators Arithmetic Operators( Mathematical) Symbol Description Example 1 Example 2 + Addition >>>55+45 100 >>> “Good” + “Morning” GoodMorning   -   Subtraction >>>55-45 10 >>>30-80 -50   *   Multiplication >>>55*45 2475 >>> “Good” * 3 GoodGoodGood   /   Division >>>17/5 3 >>>17/5.0 3.4 >>> 17.0/5 3.4 >>>28/3 9   %   Remainder/ Modulo >>>17%5 2 >>> 23%2 1   **   Exponentiation >>>2**3 8 >>>16**0.5 4.0 >>>2**8 256   //   Integer Division