Mohd Mohtashim has Published 251 Articles

Updating Strings in Python

Mohd Mohtashim

Mohd Mohtashim

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

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

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

432 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

Loop Control Statements in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 12:04:27

494 Views

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.Python supports the following control statements. Click the following links to check their detail.Let us go through the loop control statements brieflySr.NoOperator & Description1break statementTerminates the ... Read More

Python Membership Operators

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 12:00:24

109 Views

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −Sr.NoOperator & DescriptionExample1inEvaluates to true if it finds a variable in the specified sequence and false otherwise.x in y, here in results in a 1 if x ... Read More

Data Type Conversion in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 11:41:39

9K+ Views

Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.Sr.No.Function & Description1int(x [, ... Read More

Dictionary Data Type in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 11:32:11

13K+ Views

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.ExampleDictionaries are ... Read More

Tuple Data Type in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 11:31:28

767 Views

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.ExampleThe main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their ... Read More

List Data Type in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 11:30:34

4K+ Views

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different ... Read More

Multiple Statements in Python

Mohd Mohtashim

Mohd Mohtashim

Updated on 24-Jan-2020 11:30:19

4K+ Views

Multiple Statements on a Single LineThe semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon −import sys; x = 'foo'; sys.stdout.write(x + '')Multiple Statement Groups as SuitesA group of individual statements, ... Read More

Advertisements