Python | Set 3 (Strings, Lists, Tuples, Iterations)
Last Updated: 20-10-2020
In the previous article, we read about the basics of Python. Now, we continue with some more python concepts.
Strings in Python
A string is a sequence of characters. It can be declared in python by using double-quotes. Strings are immutable, i.e., they cannot be changed.
Assigning string to a variable
a = "This is a string"
print (a)
Lists in Python
Lists are one of the most powerful tools in python. They are just like the arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.
# Declaring a list
L =
[1, "a"
, "string"
, 1+2]
print
L
L.append(6)
print
L
L.pop()
print
L
print
L[1]
The output is :
[1, 'a', 'string', 3]
[1, 'a', 'string', 3, 6]
[1, 'a', 'string', 3]
a
Tuples in Python
A tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception that tuples cannot be changed once declared. Tuples are usually faster than lists.
Example
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)