Data structures - String formatting | User Input | Lists | sets and tuple
Default Data Structures
String formatting:
first_name = "Puneet"
print(first_name)
# It will print the name in console.
print("Hi " + first_name)
# It will concatenate the string at the time of display.
print(f"Hi {first_name}")
print("Hi {first_name}")
sentence = "Hi {}"
print(sentence.format(first_name))
last_name="Kumar Singh"
sentence2="Hi {} {}"
print(sentence2.format(first_name, last_name))
print(f"Hi {first_name} {last_name}, I hope you are learning")
Result of all print statement in sequence:
Puneet
Hi Puneet
Hi Puneet
Hi {first_name}
Hi Puneet
Hi Puneet Kumar Singh
Hi Puneet Kumar Singh, I hope you are learning
I hope this small tutorial will help to understand string formatting in python and use of f"" for format during print.
User input:
first_name=input("Enter your name: ")
print(first_name)
days=input("How many days you have worked: ")
print(days)
print(f"Hi {first_name}, you have worked for {days} days")
Sample problem solution:
first_name=input("Enter your name: ")
print(first_name)
days=input("How many days you have worked: ")
print(days)
print(type(days))
print(f"Hi {first_name}, you have worked for {days} days")
weeks=int(days) / 7
print(f"Hi {first_name}, you have worked for {weeks} weeks")
type is the keyword to know datatype of a variable.
Lists:
Index of list is always start with 0. Collection of elements in an array.
If you will pass [-1] in index then data will iterate from last location rather than from 0th index.
# defining a list
mylist = [80,96,72,100,8]
print(mylist)
# list is mutable and appending data into it, 1000 data will be added to the end of list
mylist.append(1000)
print(mylist)
# list is mutable and inserting data into it, 78758 will be added to the index 2 of list
mylist.insert(2,78758)
print(mylist)
# it will remove 8 element not the index 8
mylist.remove(8)
print(mylist)
# It will remove the element at index 2
mylist.pop(2)
print(mylist)
# sorting the list in ascending order
mylist.sort()
print(mylist)
# sorting the list in descending order
mylist.sort(reverse=True)
print(mylist)
peopleList = ["Puneet", "Rahul", "Rupesh", "Ravi"]
print(peopleList)
# index level access to list data
print(peopleList[0])
print(peopleList[1])
print(peopleList[2])
print(peopleList[3])
# slicing - you can give any range of index but index should in range of list
print(peopleList[0:2])
#reverse printing order of list
print(peopleList[-2])
# list length
print(len(peopleList))
# length of list element at index 0
print(len(peopleList[0]))
Sets:
Are list but with no order and can't contain duplicate values.
# defining a set
# set is unordered collection of unique elements and they are even not memory ordered
# set is mutable and it can be changed
mySet = {1,100,2,3,4,5,1,2}
print(mySet)
# size of set
print(f"size of set: ",len(mySet))
for x in mySet:
print(x)
# discarding the element from set
mySet.discard(100)
print(mySet)
# removing the element from set
mySet.remove(1)
print(mySet)
# clearing the set
# it will remove all the elements from set
mySet.clear()
print(mySet)
# adding the element to set
mySet.add(100)
print(mySet)
# adding multiple elements to set
# it will add the elements to set
mySet.update({1,2,3,4,5})
print(mySet)
# adding multiple elements to set
mySet.update([6,4,3,9,8])
print(mySet)
Tuples:
# tuples are immutable sequences in Python
# they are defined using parentheses ()
# tuples can contain any type of object
# tuples can be nested
# tuples can be unpacked
# tuples can be used as keys in dictionaries
# tuples can be used as elements in sets
# tuples can be used as elements in frozensets
# tuples can be used as elements in lists
# tuples can be used as elements in arrays
# tuples can be used as elements in numpy arrays
# tuples can be used as elements in pandas dataframes
# tuples can be used as elements in pandas series
# tuples can be used as elements in pandas panels
# tuples can be used as elements in pandas panels
myTuple = (1, 2, 3, 4, 5)
print(myTuple)
# type of variable
print(type(myTuple))
# length of tuple
print("lenght of tuple:",len(myTuple))
# index level access to tuple data
print(myTuple[0])
print(myTuple[1])
List Assignment for testing learning:
"""
Create a list of 5 animals called zoo.
Delete the animal at 3rd index.
append a new animal to the end of the list.
Delete the animal at the beginning of the list.
print all elements of the list.
print only the first 3 animals of the list.
"""
zoo = ["Lion", "Tiger", "Elephant", "Giraffe", "Zebra"]
print("Original zoo list:", zoo)
# Delete the animal at 3rd index
# zoo.pop(3) will also do the same thing.
del zoo[3]
print("After deleting the animal at 3rd index:", zoo)
# Append a new animal to the end of the list
zoo.append("Panda")
print("After appending a new animal:", zoo)
# Delete the animal at the beginning of the list
del zoo[0]
print("After deleting the animal at the beginning:", zoo)
# Print all elements of the list
print("All animals in the zoo:", zoo)
# Print only the first 3 animals of the list
print("First 3 animals in the zoo:", zoo[:3])
Comments
Post a Comment