Mohd Mohtashim has Published 238 Articles

Basic List Operations in Python

Mohd Mohtashim

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

Delete List Elements in Python

Mohd Mohtashim

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

Accessing Values of Lists in Python

Mohd Mohtashim

Mohd Mohtashim

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

477 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

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

Unicode String in Python

Mohd Mohtashim

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, ... 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

3K+ 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

Updating Strings in Python

Mohd Mohtashim

Mohd Mohtashim

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

3K+ Views

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example −Example Live Demo#!/usr/bin/python var1 = 'Hello World!' print "Updated String :- ", var1[:6] + 'Python'OutputWhen the above code is ... Read More

Accessing Values of Strings in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Jan-2020 11:44:45

2K+ Views

Python does not support a character type; these are treated as strings of length one, thus also considered a substring.ExampleTo access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example − Live Demo#!/usr/bin/python var1 = 'Hello World!' var2 = "Python Programming" ... Read More

Random Number Functions in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Jan-2020 11:43:01

522 Views

Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions that are commonly used.Sr.NoFunction & Description1choice(seq)A random item from a list, tuple, or string.2randrange ([start, ] stop [, step])A randomly selected element from range(start, stop, step)3random()A random float r, such that 0 is less ... Read More

Previous 1 ... 6 7 8 9 10 ... 24 Next
Advertisements