Python (Strings, Lists, Tuples, Iteration)
In my previous articles we have read about Basic python functions and there applications.
Now we will learn more about concepts of python,
Strings in Python
A string is a sequence of characters. It can be declared in python by using double quotes or by single quotes, Strings are immutable, i.e., they cannot be changed once declared.
Lets have a look on the syntax :
# Assigning string to a variable
# and printing the message
a
=
"This is a string"
print
a
output :
This is a string
Lists in Python
Lists are one of the most powerful tools in python. They are mostle like the arrays declared in other languages. But the most powerful thing is that list need not to be always homogenous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable so, they can be altered once declared.
Note : The syntax of defining the Lists is square brackets "[]".
Syntax :
# Declaring a list
L
=
[
1
,
"a"
,
"string"
,
1
+
2
]
print
L
L.append(
6
)
print
L
L.pop()
print
L
print
L[
1
]
output :
[1, 'a', 'string', 3]
[1, 'a', 'string', 3, 6]
[1, 'a', 'string', 3]
a
Note :
we will discuss about the two methods which is used in our Example.
Append Method :
The append() method in python adds a single item to the existing list. It doesn't return a new list of items but will modify the original list by adding the item to the end of the list.
POP Method :
pop() is an inbuilt function in Python that removes and returns last value from the list or the given index value. ... If the index is not given, then the last element is popped out and removed from the list.
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, they are immutable as mentioned. Tuples are usually much faster than lists.
Note : They are define by the parenthesis "()"
syntax :
tup
=
(
1
,
"a"
,
"string"
,
1
+
2
)
print
tup
print
tup[
1
]
output :
(1, 'a', 'string', 3)
a
Lets see some other Examples :
my_data = ("hi", "hello", "bye")print(my_data)
output :
('hi', 'hello', 'bye')
Iterations in Python
Iterations or looping can be performed in python by ‘for’ and ‘while’ loops. Apart from iterating upon a particular condition, we can also iterate on strings, lists, and tuples.Example1: Iteration by while loop for a condition
i
=
1
while
(i <
10
):
i
+
=
1
print
i,
output :
2 3 4 5 6 7 8 9 10
Example 2: Iteration by for loop on string
s
=
"Hello World"
for
i
in
s :
print
i
output :
H
e
l
l
o
W
o
r
l
d
Example 3: Iteration by for loop on list
L
=
[
1
,
4
,
5
,
7
,
8
,
9
]
for
i
in
L:
print
i,
output :
1 4 5 7 8 9
Example 4 : Iteration by for loop for range
for
i
in
range
(
0
,
10
):
print
i,
output :
0 1 2 3 4 5 6 7 8 9
In this article i have given a brief introduction on Python data types.
Next Article : String in Python
Recommended Post : Python 3 basic (print function)
#anaconda python
#opencv
#matplotlib
#python online
#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
#best course to learn python
#learn python with projects
#python artificial intelligence tutorial
#micropython #python for data science
3 Comments
Impressive content sir
ReplyDeleteWell written sir 🙂
ReplyDeleteUseful information!
ReplyDeletePython Online Training Course
Python Developer Course Online Training
If you have any doubt, Please let me know.