0% found this document useful (0 votes)
438 views15 pages

Artificial Intelligence Lab Manual: Python

This document provides an introduction to Python data types, containers, input/output, and operators. It discusses Python strings, including different string syntaxes and methods. It also covers Python lists, including indexing, slicing, adding/removing elements, and converting lists to dictionaries. The document proposes several lab tasks to practice working with strings and lists in Python.

Uploaded by

Aiman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
438 views15 pages

Artificial Intelligence Lab Manual: Python

This document provides an introduction to Python data types, containers, input/output, and operators. It discusses Python strings, including different string syntaxes and methods. It also covers Python lists, including indexing, slicing, adding/removing elements, and converting lists to dictionaries. The document proposes several lab tasks to practice working with strings and lists in Python.

Uploaded by

Aiman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Artificial Intelligence

Lab Manual

Python

Lab # 03
Python Basics

Shakeel Ahmad
LAB # 3: Data types, Containers, Input/output, and Operators in Python.

Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes or even triple quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """. A string in Python consists of a
series or sequence of characters - letters, numbers, and special characters. Strings can be indexed - often
synonymously called subscripted as well. Similar to C, the first character of a string has the index 0.

Different string syntaxes (simple, double or triple quotes):


>>>s = 'Hello, how are you?’ s = "Hi, what's up"
# string to span more than one line
>>>s = '''Hello, # tripling the quotes allows the how are
you‘’’
>>>s = """Hi,
what's up?"""
The newline character is \n, and the tab character is \t. Strings are collections like lists. Hence, they can be indexed and
sliced, using the same syntax and rules.

Indexing:
>>>a = "hello"
>>> a[0]
>>> a[1]
>>> a[-1]
print ( a[-1] )
o(Remember that negative indices correspond to counting from the right end.)

Slicing:

>>> a = "hello, world!"


>>> a[3:6] # 3rd to 6th (excluded) elements: elements 3, 4, 5
>>> a[2:10:2] # Syntax: a[start:stop:step]
>>> a[::3] # every three characters, from beginning to end
Accents and special characters can also be handled in Unicode strings. A string is an immutable object and it
is not possible to modify its contents. One may however create new strings from the original one.
>>>a = "hello, world!"
>>>a[2] = 'z'
>>>a.replace('l', 'z', 1)
>>>a.replace('l', 'z')
Strings have many useful methods, such as a.replace as seen above. Remember the a.object-oriented notation
and use tab completion or help(str) to search for new methods. See also: Python offers advanced possibilities
is referred to
for manipulating strings, looking for patterns or formatting. The interested reader
https://docs.python.org/library/stdtypes.html#stringmethods and
https://docs.python.org/library/string.html#new-string-formatting
String formatting:
>>> 'An integer: %i; a float: %f; another string:
%s' % (1, 0.1, 'string')
>>> i = 102
>>> filename = 'processing_of_dataset_%d.txt' % i

Python List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very
flexible. All the items in a list do not need to be of the same type. Declaring a list is , Items
separated by commas are enclosed within brackets [ ].
>>> colors = ['red', 'blue', 'green', 'black', 'white']
>>> type(colors)

Indexing: accessing individual objects contained in the list:

Indexing starts at 0 (as in C), not at 1 (as in Fortran or Matlab)!,


Slicing: obtaining sublists of regularly-spaced elements. Note that colors[start:stop] contains the elements
with indices i such as start<= i < stop (i ranging from start to stop-1). Therefore, colors[start:stop] has (stop -
start) elements. Slicing syntax: colors[start:stop:stride] All slicing parameters are optional.
Lists are mutable objects and can be modified:

Note: The elements of a list may have different types:


Add and remove elements:
Reverse:
Concatenate and repeat lists:
Sort:

Methods and Object-Oriented Programming


The notation rcolors.method() (e.g. rcolors.append(3) and colors.pop()) is our first
example of object- oriented programming (OOP). Being a list, the object rcolors owns
the method function that is called using the notation.
>>>rcolors.<tab> # views the list of OOP function that can
be performed on this object.
Lab Tasks:
1.Write a Python program to sum all the items in a list.
2.Write a Python program to get the largest number from a list.
3.Write a Python program to remove duplicates from a list
4.Write a Python program to convert list to list of dictionaries.
Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000", "#FFFF00"]
Expected Output: [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_code': '#FF0000'},
{'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code':
'#FFFF00'}]
5.Write a Python program to read a matrix from console and print the sum for each column. Accept matrix rows, columns
and elements for each column separated with a space(for every row) as input from the user.
Input rows: 2

14
Input columns: 2
Input number of elements in a row (1, 2, 3): 1 2
34
sum for each column: 4 6
6. Write a Python program to Zip two given lists of lists. Original lists:
[[1, 3], [5, 7], [9, 11]]
[[2, 4], [6, 8], [10, 12, 14]]
Zipped list:
[[1, 3, 2, 4], [5, 7, 6, 8], [9, 11, 10, 12, 14]]

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy