Found 10476 Articles for Python

Updating Lists in Python

Gireesha Devara
Updated on 14-Apr-2025 16:24:29

51K+ Views

In Python, lists are one of the built-in data structures that are used to store collections of data. The lists are mutable, which means we can modify its element after it is created. You can update single or multiple list elements using append, insert, extend, remove, and clear to change the list content by adding, updating, or removing elements from the list object. In this article, we will discuss how we can update an existing element in the list. The list is an index-based sequential data structure so that we can access the list elements by their index position, ... Read More

Accessing Values of Lists in Python

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

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

Triple Quotes in Python

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 characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up. """ print para_strOutputWhen the above code is ... Read More

String Special Operators in Python

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] will give e4[ : ]Range Slice - Gives the characters from the given rangea[1:4] will give ell5inMembership - Returns true if a character exists in the given stringH in a will give 16not inMembership - Returns true if a character does not exist in the given stringM not in a ... Read More

Updating Strings in Python

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 executed, it produces the following result −Updated String :- Hello Python

Accessing Values of Strings in Python

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" print "var1[0]: ", var1[0] print "var2[1:5]: ", var2[1:5]OutputWhen the above code is executed, it produces the following result −var1[0]: H var2[1:5]: ytho

Mathematical Constants in Python

Vikram Chiluka
Updated on 25-Oct-2022 08:30:08

1K+ Views

In this article, we will go through Python's mathematical constants and how to use them. Some of the defined constants that can be utilized in a variety of mathematical operations are included in the math module. These mathematical constants return values that are equivalent to their standard defined values. The following math module constants are available in the Python programming language: Python math.e constant Python math.pi constant Python math.tau constant Python math.inf constant Python math.nan constant Python math.e constant The Euler's number, 2.71828182846, is returned by the math.e constant. Syntax math.e Return Value − It returns the ... Read More

Random Number Functions in Python

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

528 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 than or equal to r and r is less than 14seed([x])Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.5shuffle(lst)Randomizes the items of a list in place. Returns None.6uniform(x, y)A random float r, such that x is less ... Read More

Advertisements