Class - 6 PYTHON STRINGS, LIST, TUPLE, DICTIONARY

Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Class - 6 PYTHON STRINGS, LIST, TUPLE, DICTIONARY Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

PYTHON STRINGS, LIST, TUPLE, DICTIONARY

Notes from the AI Basic Course (Class 6) by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)

Google Colab link:

https://colab.research.google.com/drive/1yQX7lohy2HZlQ-x6_53yUBox2Fio0R_T

print("Hello world")

print('Hello world')

a = "Hello"

print(a)

a = """AI, or Artificial Intelligence, refers to the development and implementation of computer systems

or machines that can perform tasks that would typically require human intelligence.

It is a multidisciplinary field that encompasses various subfields, such as machine learning,

natural language processing, computer vision, robotics, and more."""

print(a)

Upper Case:

a = "Hello, World!"

print(a.upper())

Lower Case:

a = " Hello, World! "

print(a.lower())

Remove Whitespace:

a = " Hello, World! "

print(a.strip()) # returns "Hello, World!"

Replace String:

a = "Hello, World!"

print(a.replace("H", "J"))

Split String:

a = "Hello, World!"

print(a.split(",")) # returns ['Hello', ' World!']

Task#01:

Perform the following tasks using the provided built-in functions to manipulate the string input_string = " AI and related technologies have a positive impact on the way the IT sector works. ":

a) Convert the input_string to uppercase using the "upper()" function and store the result in a variable called "capitalized_text".

b) Convert the input_string to lowercase using the "lower()" function and store the result in a variable called "lowercase_text".

c) Remove the White spaces from the input_string using the "strip()" function and store the result in a variable called "White_space"

d) Split the input_string into a list of words using the "split()" function and store the result in a variable called "word_list".

e) Replace the word 'positive' with the word 'remarkable' in the input_string using the "replace()" function and store the result in a variable called "replaced_text".

List:

thislist = ["apple", "banana", "cherry"]

print(thislist)

List Items:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]

print(thislist)

List Length:

print(len(thislist))

List Items - Data Types:

# String, int and boolean data types:

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False]

# A list with strings, integers and boolean values:

list1 = ["abc", 34, True, 40, "male"]

print(list1)

Access Elements:

#Access the first element of list

print(list1[0])

#Access the last element of list

print(list1[-1])

Slicing:

print(list_of_integer[1:3])

Add new elements in the list:

#Add new element in the list using append

list_of_integer.append(6)

print(list_of_integer)

Reversing a List:

list_of_integer.reverse()

print(list_of_integer)

Removing Elements from the List:

print("Initial List: ")

print(list_of_integer)

# Removing elements from List

# using Remove() method

list_of_integer.remove(5)

print("\nList after Removal of one element: ")

print(list_of_integer)

Create a Tuple:

thistuple = ("apple", "banana", "cherry")

print(thistuple)

Tuple Items:

# Tuples allow duplicate values:

thistuple = ("apple", "banana", "cherry", "apple", "cherry")

print(thistuple)

Create Tuple With One Item:

# One item tuple, remember the comma:

thistuple = ("apple",)

print(type(thistuple))

#NOT a tuple

thistuple = ("apple")

print(type(thistuple))

Tuple Items - Data Types:

# String, int and boolean data types:

tuple1 = ("apple", "banana", "cherry")

tuple2 = (1, 5, 7, 9, 3)

tuple3 = (True, False, False)

# A tuple can contain different data types:

# A tuple with strings, integers and boolean values:

tuple1 = ("abc", 34, True, 40, "male")

Create a Set:

thisset = {"apple", "banana", "cherry"}

print(thisset)

Duplicates Not Allowed:

# Duplicate values will be ignored:

thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)

Get the Length of a Set:

# Get the number of items in a set:

thisset = {"apple", "banana", "cherry"}

print(len(thisset))

Set Items - Data Types:

# String, int and boolean data types:

set1 = {"apple", "banana", "cherry"}

set2 = {1, 5, 7, 9, 3}

set3 = {True, False, False}

# A set can contain different data types:

# A set with strings, integers and boolean values:

set1 = {"abc", 34, True, 40, "male"}

print(set1)

type():

# What is the data type of a set?

myset = {"apple", "banana", "cherry"}

print(type(myset))

Python Dictionaries:

# Dictionaries are written with curly brackets, and have keys and values:

# Create and print a dictionary:

thisdict = {"brand": "Ford", "model": "Mustang","year": 1964}

print(thisdict)

Dictionary Items:

# Print the "brand" value of the dictionary:

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

print(thisdict["brand"])

Duplicates Not Allowed:

# Duplicate values will overwrite existing values:

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964,

"year": 2020

}

print(thisdict)

Python If ... Else:

# If statement:

a = 330

b = 200

if b > a:

print("b is greater than a")

Indentation:

a = 33

b = 200

if b > a:

print("b is greater than a") # you will get an error

Elif:

a = 33

b = 33

if b > a:

print("b is greater than a")

elif a == b:

print("a and b are equal")

Else:

a = 200

b = 33

if b > a:

print("b is greater than a")

elif a == b:

print("a and b are equal")

else:

print("a is greater than b")

And:

a = 200

b = 330

c = 500

if a > b and c > a:

print("Both conditions are True")

Or:

a = 200

b = 33

c = 500

if a > b or a > c:

print("At least one of the conditions is True")

Not:

# Test if a is NOT greater than b:

a = 33

b = 200

if not a > b:

print("a is NOT greater than b")

Tuples are immutable (not changeable)

() brackets are used for tuples

In list, values can be changed.

Sets are un-ordered & un-indexed, un-changeable.

{} brackets are used for sets

Sets doesn't allow duplication.

Python Dictionaries:

Used to store data values in key value pairs.

Equals: ==

Not Equals: !=

Elif:

For two conditions, if one is wrong follows the second one.

#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #hamzanadeem

gold foxx

Student at Islamia College,Peshawar

1 年

Bhai me abhi 1st year me hoon mujhe Coding nhi aati liken mujhe AI seekni hai Aap please guide kre Me kia karoo

回复

要查看或添加评论,请登录

Hamza Nadeem的更多文章

社区洞察

其他会员也浏览了