10

Click here to load reader

Python strings List | Python training in Bangalore -Learnbay.in

Embed Size (px)

Citation preview

Page 1: Python strings List | Python training in Bangalore -Learnbay.in

Class4- String ,Lists

Class4www.learnbay.in - Python training in Bangalore

Page 2: Python strings List | Python training in Bangalore -Learnbay.in

Strings

A String is sequence of character.

How to create a string?

# all of the following are equivalentmy_string = 'Hello'

my_string = "Hello"

my_string = '''Hello'''

# Multiple line either single triple quote or single double quote

my_string = """Hello, welcome to

the world of Python"""

del my_string

Page 3: Python strings List | Python training in Bangalore -Learnbay.in

Strings ..

Print “\\”

Print r’\\’

print 'C:\\abcd'

print r'C:\\abcd'

Access Characters in string?

Strings are immutable.

Indexing: Access individual elements of string.

Index must be an integer.Accessing character out of index will raise “IndexError”

Python Allows negative indexing for its sequences

*****Try few negative indexes and figure out what negative index does*****

Output??

s = 'kri'

a,b,c=s

print a+b+c

print b

print c

Page 4: Python strings List | Python training in Bangalore -Learnbay.in

Strings-Slicing

String = “abc ghijklm”

String[start:end:stride]

End is up to but not including the character at position end.

If start is omitted- defaults to 0

If end is omitted -defaults to end of string

If stride is omitted it defaults to 1

Do exercises on the above.Put end as -ve, stride as negative and then check

https://repl.it/FWyl/0 : Code example - String slicing

Page 5: Python strings List | Python training in Bangalore -Learnbay.in

String: Operations

Try out “+,*,in and not in” on strings.

Try out these builtin methods:

capitalize() ,center(width, fillchar),

count(str, beg= 0,end=len(string)) : Counts how many times str occurs in string or in a substring of string

find(str, beg=0, end=len(string)) :Determine if str occurs in string or in a substring of string.Return index if

found else -1.

index(str, beg=0, end=len(string)): same as find but return exception if not found

https://repl.it/GFc2/8 : code example- String Methods

https://repl.it/FXnj/3: Reverse a string

Page 6: Python strings List | Python training in Bangalore -Learnbay.in

List

List is a sequence of values.

Items in a list need not be of the same type.

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"]

List4 = [‘a’,’b’,[1.0,2]]

Lists are mutable. Exercise:- Take a list and change/Update one member of list.

Also do some example on “in” operator in the list.

Page 7: Python strings List | Python training in Bangalore -Learnbay.in

List …..

Delete from list:

list1 = ['physics', 'chemistry', 1997, 2000];

print list1

del list1[2];

print "After deleting value at index 2 : "

print list1

Try +,*,len ,in operations on list and also slicing on Lists.

t=[1,2,3,4,5] t[1:3]=[8,8] print t

Page 8: Python strings List | Python training in Bangalore -Learnbay.in

Lists Methods

Try examples on theses methods:

Append ,extend ,sort,pop etc

Cmp,len,max,min etc

List Code example1: https://repl.it/FXmp/8

Example2: https://repl.it/FXkk/0

Example: https://repl.it/Hk1i/0

Example3; https://repl.it/GQlF/2 -Reverse list

Page 9: Python strings List | Python training in Bangalore -Learnbay.in

LIst Comprehension

List Comprehension Provides a concise way to Create a list.

It Consists of brackets ,expressions followed by a for clause, then

zero or more for or if clauses.

Returns a new list

Basic Syntax is :

[ expression for item in list if conditional ]

Page 10: Python strings List | Python training in Bangalore -Learnbay.in

Examples of List Comprehension

List = [i for i in range(10)]

squares = [x**2 for x in range(10)]

[x+y for x in [10,30,50] for y in [20,40,60]]

[x.upper() for x in ["a","b","c"]]

List Comprehension Code Example: https://repl.it/GQk9/3

List to str: https://repl.it/GQlO/1