Mohd Mohtashim has Published 251 Articles

Delete Tuple Elements in Python

Mohd Mohtashim

Mohd Mohtashim

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

7K+ 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 ... Read More

Accessing Values of Tuples in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Jan-2020 12:40:30

8K+ Views

To access values in tuple, 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 tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5, 6, 7 ); print "tup1[0]: ", tup1[0]; print "tup2[1:5]: ", tup2[1:5];OutputWhen the ... Read More

Built-in List Functions & Methods in Python

Mohd Mohtashim

Mohd Mohtashim

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

18K+ 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 ... Read More

Basic List Operations in Python

Mohd Mohtashim

Mohd Mohtashim

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

1K+ 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, ... Read More

Delete List Elements in Python

Mohd Mohtashim

Mohd Mohtashim

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

313 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 : " ... Read More

Accessing Values of Lists in Python

Mohd Mohtashim

Mohd Mohtashim

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

321 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 ... Read More

Built-in String Methods in Python

Mohd Mohtashim

Mohd Mohtashim

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

2K+ 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 ... Read More

Unicode String in Python

Mohd Mohtashim

Mohd Mohtashim

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

2K+ 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, ... Read More

Triple Quotes in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Jan-2020 12:22:40

6K+ Views

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.The syntax for triple quotes consists of three consecutive single or double quotes.Example Live Demo#!/usr/bin/python para_str = """this is a long string that is made up of several lines and non-printable ... Read More

String Special Operators in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Jan-2020 12:21:20

2K+ Views

Assume string variable a holds 'Hello' and variable b holds 'Python', then −Sr.NoOperator & DescriptionExample1+Concatenation - Adds values on either side of the operatora + b will give HelloPython2*Repetition - Creates new strings, concatenating multiple copies of the same stringa*2 will give-HelloHello3[]Slice - Gives the character from the given indexa[1] ... Read More

Previous 1 ... 7 8 9 10 11 ... 26 Next
Advertisements