Basic Tuple Operations in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:47:24

492 Views

Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter −Python ExpressionResultsDescriptionlen((1, 2, 3))3Length(1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Concatenation('Hi!',) * 4('Hi!', 'Hi!', 'Hi!', 'Hi!')Repetition3 in (1, 2, 3)TrueMembershipfor x in (1, 2, 3): print x,1 2 3Iteration

Delete Tuple Elements in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:41:35

8K+ Views

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.To explicitly remove an entire tuple, just use the del statement.Example Live Demo#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup : "; print tup;OutputThis produces the following result. Note an exception raised, this is because after del tup tuple does not exist any more −('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in print tup; NameError: name 'tup' is not defined

Built-in List Functions and Methods in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:40:01

22K+ Views

Python includes the following list functions −Sr.NoFunction with Description1cmp(list1, list2)Compares elements of both lists.2len(list)Gives the total length of the list.p>3max(list)Returns item from the list with max value.4min(list)Returns item from the list with min value.5list(seq)Converts a tuple into list.Python includes following list methodsSr.NoMethods with Description1list.append(obj)Appends object obj to list2list.count(obj)Returns count of how many times obj occurs in list3list.extend(seq)Appends the contents of seq to list4list.index(obj)Returns the lowest index in list that obj appears5list.insert(index, obj)Inserts object obj into list at offset index6list.pop(obj=list[-1])Removes and returns last object or obj from list7list.remove(obj)Removes object obj from list8list.reverse()Reverses objects of list in place9list.sort([func])Sorts objects of list, use ... Read More

Exam Guidance for CAT - Common Admission Test

Samual Sam
Updated on 28-Jan-2020 12:39:21

335 Views

In a world obsessed with the word ‘Management’, MBA has become one of the most sought after degrees. Management and entrepreneurship often go hand-in-hand. Most of the startup CEOs and corporate honchos are MBA degree holders. CAT which stands for Common Admission Test is a much bandied about entrance exam especially when it comes to aspirants. To make a mark for themselves and subsequently join elite institutions such as Indian Institutes of Managements, Faculty of Management Studies etc., aspirants have to score a decent percentile in CAT. With the number of students taking CAT escalating to new heights, the sense ... Read More

Basic List Operations in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:39:15

2K+ Views

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.Python ExpressionResultsDescriptionlen([1, 2, 3])3Length[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]Concatenation['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition3 in [1, 2, 3]TrueMembershipfor x in [1, 2, 3]: print x,1 2 3Iteration

Delete List Elements in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:33:29

344 Views

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.Example Live Demo#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1[2]; print "After deleting value at index 2 : " print list1OutputWhen the above code is executed, it produces the following result −['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]Note − remove() method is discussed in subsequent section.

Accessing Values of Lists in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:32:04

487 Views

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.Example Live Demo#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]OutputWhen the above code is executed, it produces the following result −list1[0]: physics list2[1:5]: [2, 3, 4, 5]

Built-in String Methods in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:31:33

4K+ Views

Python includes the following built-in methods to manipulate strings −Sr.NoFunction & Description1capitalize()Capitalizes first letter of string2center(width, fillchar)Returns a space-padded string with the original string centered to a total of width columns.3count(str, beg= 0, end=len(string))Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.4decode(encoding='UTF-8', errors='strict')Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding.5encode(encoding='UTF-8', errors='strict')Returns encoded string version of string; on error, default is to raise a ValueError unless errors is given with 'ignore' or 'replace'.6endswith(suffix, beg=0, end=len(string))Determines if string or ... Read More

Unicode String in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:30:51

3K+ Views

Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world. I'll restrict my treatment of Unicode strings to the following −Example Live Demo#!/usr/bin/python print u'Hello, world!'OutputWhen the above code is executed, it produces the following result −Hello, world! As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.

Facebook Introduces Video as Your Profile Pic

Samual Sam
Updated on 28-Jan-2020 12:29:39

286 Views

Now you can upload a 7-second video as your profile pic. Isn’t that great? You can upload a fun-filled video or just a formal introduction in just that small little profile space describing you.Along with the profile video, you can now, highlight your bio-data with a 100-character bio field.And we have an updated mobile-centric design with centered profile photos. You want more? Facebook has Big sections for photos and friends, temporary profile pics, option to pin feature photos to the top of the profile! What more can you ask for?This helps you to highlight the most important things in your ... Read More

Advertisements