Python Lab 2: Basics of Programming in Python
Variables in Python
Variables in python works more like tags unlike containers in other programming languages. If you change the value of the variable, it just changes the tag to the new value in memory. id() function is used to check the memory location.
x = 5
x=y=z=5
id(x)
Data types
Int → Integer data type specifies that some variable points to a data value which is an integer.
Float → Float data type indicates that some variable points to a data value which is a decimal point number
Complex → Complex data type indicates that the variable points to a complex number
Strings → String data type indicates that some variable points to a data value which is alphabetic.
type() function is used to check the data type of given variable
x = 5
type(x)
y=5.5
type(y)
z='Hello'
type(z)
a=1+5j
type(a)
Data Type Conversion
Converting Integer into Float
x=5
y = float(x)
type(y)
Converting Float into Integer
x=5.5
y=int(x)
type(y)
Converting Integer into Complex
x=5
y=complex(x)
type(y)
Converting Integer into String
x=5
y=str(x)
type(y)
Math operators
max() function compares and tells the maximum number among given numbers.similarly min() function compares and tells the smallest number among given numbers
max(10,20,100)
min(10,20,100)
pow() function returns value of x to the power of y.In below example x=5 and y=3
pow(5,3)
abs() function returns the absolute value of given number, the number can be integer or float or even a complex number
abs(-8)
‘==’ is used as a comparison operator.It compares the LHS with RHS and returns a true value if both sides are equal
50==50
Escape Sequence
- \n :- Next line or new line character
- \t :- Tab character
- \ :- Continuation
my\_string='RSTFORUM \\t is Baap of networking’
my\_string='RSTFORUM \\n is Baap \\n of networking’
my\_string='RSTFORUM is Baap\\'s of networking’
String Functions
x = 'CISCO Routers'
index() function is used to identify the index value of given sub string
x.index('R')
find() function is also used to find the index value of given sub string but it doesn’t return an error if sub string is not found instead it returns -1.
x.find('z')
count() function tells the number of times a sub string has occurred
x.count('C')
upper() function is used to convert the string into upper case, if the string is already in upper case then it will remain unchanged
x.upper()
lower() function is used to convert the string into lower case, if the string is already in lower case then it will remain unchanged
x.lower()
startswith() function returns a true value if the string starts with given sub string or else returns False
x.startswith('C')
endswith() function returns a true value if the string ends with given sub string or else returns False
x.endswith('ers')
replace() function is used to replace a sub string with new string
x.replace('CISCO','JUNIPER')
split() function converts a given string into list ,when called by default it splits the string at white space.We can optionally mention the delimiter where we want the string to be split
x.split()
above code splits the string at white space in between CISCO and Routers.If the string is separated by a ‘,’ instead of white space then we can use ‘,’ as separator or delimiter.Same is shown in below example
x = 'CISCO,Routers''
x.split(',')
Optionally we can also the decide the number of index values in which the string will be splitted. This is called as maxsplit number.
x='CISCO Routers and Switches'
x.split(' ',2)
strip() function is used to strip blank space from start and end of the string.
x=' CISCO Routers '
x.strip()
join() function is used to convert list into string.You can specify the delimiter with which you want to join the string.In below example we are joining the string with a ‘,’.
x=\['CISCO','Routers','Switches'\]
','.join(x)
len() function returns the number of characters in the sting
len(x)
The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false if it doesn’t find
'CISCO' in x
The ‘not in’ operator returns true if the given value is not present in the string
'Juniper' not in x
Value substitution in String
x = 'my name is %s and my age is %d'
x%('Rahul',20)
x = 'my name is {} and my age is {}'
x.format('Rahul',20)
String concatenation and multiplication
x = 'Python'+'is'+'best'+'programming language' x = 'Python'*3```
#### String Slicing
x = 'RST FORUM'
the number before ‘:’ indicated the start point of slicing and after ‘:’ indicates the end point of slicing.The slicing will stop one index before the ends point mentioned in the slicing.In below example slicing will start at index value 2 and will stop at index value 4
x[2:5]
the 3rd number after’:’ indicates the period or interval of slicing.In below example number 2 indicates that slicing will happen at an interval of 2 i.e every alternate sub string will be printed as output
x[2:7:2]
slicing with negative index values
x[-7:-2]
slicing in reverse direction with period of 2
x[8:2:-2]