Python-Workbook 2
Python-Workbook 2
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Simple Output
Quick Reference x in y results in True if x is a member of sequence y. print(x) print the value in x and a new line.
x not in y results in True if x is not a member of sequence y. prin(x, y) print the value in x and a space.
print(x,y, sep=“...”) prints the values of x, y separated by “...”
stores the integer in x Identity Operators instead of the default space. print(x, y, sep=“ ’’, end = “::”)
x is y Evaluates to True if the variables on either side of the prints the values of x, y seperated by a tab and instead of
stores the float in y
operator point to the same object. ending with a newline
s = “Hello World” stores string Hello World in s
x is not y Evaluates to False if the variables on either side
of the op-erator point to the same object.
Returns Boolean values True or False statement, while statement, for statement or a def statement
x == y checks if x is equal to y Similarly,
x != y checks if x is not equal to y statement
x > y checks if value in x is greater than y
x > y checks if value in x is greater than that in y p r i n t ( ‘ ‘ The v a l u e o f x i s ’ ’ , x )
x ≥ y checks if x is greater than or equal to y
x < y checks if value in x is less than that in y
x ≤ y checks if value in x is less than or equal to y Prints x
x < y < z checks if y is in between x and z
s = "I am a string"
enclosed in double quotes.
returns True if both conditions are True Simple Input s = 'He said "Good Morning", to the class’
returns True if either condition is True x = input() for taking input. use single quotes if there is a double quote in the string.
not x > negates the condition x = input("Enter number: ") display a prompt while taking s = "It's time"
input. use double quotes if there is a single quote in the string.
The value given by input is always a string.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Definingfunctions
accesses the first character in the string d e f a d d _ on e ( x ) : adds at the end of the list pr.
accesses the fifth character in the string return x + pr becomes
appends , ,
accesses the last character in the string defines the add_one function that takes one argument and pr becomes
accesses the last but one character in s. returns the value of argument plus one. Operations mentioned above modify the list itself.
d e f ge tMa x ( x , y ) :
if x > y:
return x f o r i in pr :
Slicing strings return y print ( i )
s = “Hello World”
defines the getMax function that takes two arguments and
returns “lo World” iterates over the list pr one item at a time.
returns the greater one from them.
substring from character with index to end.
returns “Hello W”
substring from start to character with index
returns “lo W” creates the dictionary
substring from character with index to gives the corresponding value,
character with index
returns “llo Wor” x = add_one(x) increments x by one.
changes the value for the key ‘mar” to
substring from third character to the third stores the return value
character from the end. biggest = getMax(biggest, currentValue)
creates the key “apr” with as the value
returns list of values,
returns list of keys,
[“jan”, “feb”, “mar”, “apr”]
s = “Hello” + ‘World” stores HelloWorld in s. creates the list pr.
len(s) length of the string s len(pr) returns the length of the list,
"ell" in s checks for the presence of “ell” in s. checks for the presence of in the list pr.
s.lower() returns “helloworld” adds the lists and returns a new list.
creates the set and stores in prs.
a new string with characters of s, in lower case. ods =
s.upper() returns “HELLOWORLD” creates the set and stores in ods.
a new string with characters of s, in upper case. accesses the first item, . prs | ods gives the union of the sets,
s.replace(“l”, “m”) returns “Hemmo Wormd” accesses the fourth item from end, . prs & ods gives the intersection of the sets,
a new string with all the l replaced with m. accesses ods - prs gives the difference of sets
s.split() returns [“Hello”, “World”] list ofitems from third to last. items in ods that are not in prs, which is
a list of words in the string. accesses ods ˆ prs gives the symmetric difference
All the above operations return new strings. list ofitems from first to fourth. items in ods or in prs but not in both,
The original string remains unaltered. accesses
list ofitems from third to fifth.
accesses
range function alternate items, starting from the second item. fi l e L o c = ‘ ‘ / home / t s p r i n t / p r i m e s . t x t ’ ’
returns list of numbers from to . f o r l i n e i n open ( fi l e L o c ) :
returns odd numbers from to . prime = i n t ( l i n e )
range returns a “generator”, convert it to list to see p r i n t ( prime * prime )
the values, Example: Data in the file is read as a string line by line.