Python Basics: Part 2

  1. New data types.

a. Bool

b. List []

Declaration

Indexing slicing stepping

Nested list. List inside a list as an element

Accessing elements inside a nested list

List concatenation

Concatenation only occurs when both variables have same data-type.

List multiplication

List mutability

  1. Tuples ()

    Declaration

    Indexing, slicing

    Mutability and Immutability

    Tuple concatenation

    Multiplication

    Typecasting from tuple to list and vice-versa

  2. Finding length using len() function

  3. Set {}

Declaration

Is set doing sorting?

Make a set with duplicate values and see the output

Indexing, slicing

Typecast a list to set

  • Set to list

  • Nested list to set

  • Tuple to set

  • Set to tuple

Set does not allow indexing because order or input and order or output is different.

  1. Dictionary {}

    A key-value pair

    Declare a dictionary

# there should not be duplicate keys, as only latest will be taken 
# key is like a index and there cannot be repeated index
# key should always be unique

Try different Key data-type values

Try different "value" data type values

Indexing, Slicing

  1. Exploring In-built function: Strings

    a. Declare a string eg. a = "Name"

    b. Call the variable add a dot next to it and press the tab key on the keyboard above the capslock key. Eg. a. # press_tab_key

    c. All inbuilt functions that Python has for the string will show in a drop-down form.

    d. Try all of them once and play around.

    e. Do try index() , find() , split() and replace() function

Does replace function makes string mutable?

  1. Exploring In-built function: List

    a. Declare a List eg. l = [1, 2, 3, 4, 'Aynat', (3+6j), 23.45, True, [1, 2, 3]]

    b. Call the variable add a dot next to it and press the tab key on the keyboard above the capslock key. Eg. l. # press_tab_key

    c. All inbuilt functions that Python has for the list will show in a drop-down form.

    d. Try all of them once and play around.

  2. Exploring In-built function: Tuple

    a. Declare a Tuple eg. t = (1, 2, 3, 4, 'Aynat', 'kimlip', (3+5j), True)

    b. Call the variable add a dot next to it and press the tab key on the keyboard above the capslock key. Eg. t. # press_tab_key

    c. All inbuilt functions that Python has for the tuple will show in a drop-down form.

    d. Try all of them once and play around.

  3. Exploring In-built function: Set

    a. Declare a set eg. s = {3,4,5,6,6,43,56,54,5,5,5,6}

    b. Call the variable add a dot next to it and press the tab key on the keyboard above the capslock key. Eg. s. # press_tab_key

    c. All inbuilt functions that Python has for the set will show in a drop-down form.

    d. Try all of them once and play around.

  4. Exploring In-built function: Dictionary

    a. Declare a dictionary eg. d = {'name': 'ayant', 'subject': ['data science', 'ml', 'ai'], 'number': 1335454}

    b. Call the variable add a dot next to it and press the tab key on the keyboard above the capslock key. Eg. d. # press_tab_key

    c. All inbuilt functions that Python has for the dictionary will show in a drop-down form.

    d. Try all of them once and play around.

Questions

Q.1 Write a Python program that takes a string as input and prints the length of the string.

Q2. Write a program that takes a string as input and checks if it is a palindrome (reads the same forwards and backwards).

Q3. Create a program that takes a string as input and removes all the spaces from it. Print the modified string without spaces.

Q4. Check if a string contains a specific word.

Q5. Count the occurrences of a word in a string.