Python Essentials Classroom Series – 27/Jul/2021

Text Strings

  • Strings in python
message = "Hello"
language = 'python'
more_info = '''Python is used in building
command line applications
desktop application
web application
machinelearning models
'''
more_info = """Python is used in building
command line applications
desktop application
web application
machinelearning models
"""
  • f-Strings: They can be used for string interpolations
name = "Ram"
course = 'Python'
message = f"Hello {name}, Im glad you started learning {course}"
  • Useful methods of string
    • endswith
    • find
    • lower
    • replace
    • split
    • startswith
    • upper
    • capitalize

Lists

  • Lists are ordered collection of arbitrary objects. We can create the list by enclosing the values in square brackets as follows
job_roles = ['Developer', 'Tester', 'Administrator']
my_values = [1, 2.0, 'fun']
  • To access the values of the list we can use index. The index of list starts from zero
job_roles[0]
job_roles[2]
  • To append elements at the end of list we use append
job_roles.append('Manager')
  • To insert an element at certain position we use insert
job_roles.insert(2, 'Team Lead')

Preview

  • To get the count of elements in the list we can use len
len(job_roles)
  • Now lets use a while to print all the elements of the list
index = 0
while index < len(job_roles):
    print(job_roles[index])
    index += 1

Preview

  • To iterate the elements in a list we can also use a for loop

for job_role in job_roles:
    print(job_role)

Preview

  • List slicing
    • Slicing is possible in lists by using the slicing operator “`start:end:step“
    • default value of start is 0 and default value step 1 Preview
    • In python lists also have negative indexes Preview Preview
  • lists are mutable they allow us to change the values
colors[0] = 'Violet'

Preview

  • empty [] indicates empty list Preview

Tuples

  • Tuple is also a collection values and it is immutable
  • Tuple is enclosed by a group of values in paranthesis
rainbow = ('Violet', 'Indigo', 'Blue', 'Green', 'Yellow', 'Orange', 'Red')
  • We can use the while or for loop like what we have done for lists to iterate the tuple Preview Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin