Malhar Lathkar has Published 50 Articles

How to convert an object x to a string representation in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 24-Feb-2020 09:54:15

183 Views

Most commonly used str() function from Python library returns a string representation of object.>>> no=100 >>> str(no) '100' >>> L1=[1, 2, 3, 4] >>> str(L1) '[1, 2, 3, 4]' >>> d={'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}"However, repr() returns ... Read More

Python tuples are immutable then how we can add values to them?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:29:08

160 Views

Python tuple is an immutable object. Hence any operation that tries to modify it (like append/insert) is not allowed. However, following workaround can be used.First, convert tuple to list by built-in function list(). You can always append as well as insert an item to list object. Then use another built-in ... Read More

How can I do Python Tuple Slicing?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:27:57

279 Views

Slicing operator can be used with any sequence data type, including Tuple. Slicing means separating a part of a sequence, here a tuple. The symbol used for slicing is ‘:’. The operator requires two operands. First operand is the index of starting element of slice, and second is index of ... Read More

How to create a tuple from a string and a list of strings in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:22:49

115 Views

The built-in function tuple() converts a Python string into tuple of individual characters. It also turns a list object into a tuple.>>> tuple("TutorialsPoint") ('T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'P', 'o', 'i', 'n', 't') >>> L1=[45, 32, 100, 10, 24, 56] >>> tuple(L1) (45, 32, 100, 10, 24, 56)

How to find the index of an item given a list containing it in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:22:14

493 Views

Position of an element in a list (any sequence data type for that matter) is obtained by index() method. This method finds first instance of occurrence of given element.>>> L1=[45, 32, 100, 10, 24, 56] >>> L1.index(24) 4

What are the differences and similarities between tuples and lists in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:21:13

672 Views

Both List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type.SimilaritiesConcatenation, repetition, indexing and slicing can be done on objects of both types>>> #list operations >>> L1=[1, 2, 3] >>> L2=[4, 5, 6] >>> ... Read More

How to get the second-to-last element of a list in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 11:04:00

21K+ Views

Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end.  As we want second to last element in list, use -2 as index.>>> L1=[1,2,3,4,5] >>> print (L1[-2]) 4

How can I get last 4 characters of a string in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:12:13

2K+ Views

The slice operator in Python takes two operands. First operand is the beginning of slice. The index is counted from left by default. A negative operand starts counting from end. Second operand is the index of last character in slice. If omitted, slice goes upto end.We want last four characters. ... Read More

Can we assign a reference to a variable in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:11:01

972 Views

Concept of variable in Python is different from C/C++. In C/C++, variable is a named location in memory. Even if value of one is assigned to another, it creates a copy in another location.int x=5; int y=x;For example in C++, the & operator returns address of the declared variable.coutRead More

How can we unpack a string of integers to complex numbers in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 20-Feb-2020 08:09:49

112 Views

A string contains two integers separated by comma. It is first split in a list of two strings having digits.>>> s="1, 2".split(", ") >>> s ['1', '2']Two items are then converted to integers and used as arguments for complex() function>>> complex(int(s[0]), int(s[1])) (1+2j)This results in unpacking of string of integers ... Read More

Advertisements