PYTHON
A) STRINGS
1. Printing a string:
For printing a simple statement (hello world) in python we use print function as below
The statement should be in exclamatory marks,
Output: hello world.
Now let’s print another statement what’s up in same manner,
Output:
Here we end up with a syntax error as above, this is because the statement (what’s up) itself have an exclamatory
mark and python identifies only ‘what’ as an input argument and ‘s up’ is outside the input so we are getting
syntax error.
To these types of cases we have many alternatives,
1. Using double exclamatory marks
By this single exclamatory mark in statement is ignored.
2. Using backslash where ever we need to ignore the mark as shown below
The output for the above two will be same
Output:
2. Length of string:
Program for finding length of string is as follows,
len(input) is the operator we use to know the length of string.
Output: or
output:
3.To print only some part of string :
a) print only first letter of string
we need to call for position of letter using square bracket starting with “zero” as first
letter. Out put :
b ) printing w , program is as follows
output:
c) printing from one letter to other
output:
d) printing from starting to some character
output:
4.some other methods on strings
Output: Prints the string in lowercase
Output: prints the string in upper case
5. Replace string with another string:
.replace is used replace one string with another.
Output:
6.Adding two strings:
and out put will be
Here there wont be any space betweenhello and jack.
for space between them we need to add ‘ space ‘
For adding strings we also use format function which is as follows
out put
output for getting uppercase to
specified string
format function is actually used if we have more number of strings to be added
syntax f ’{input1} {input2} {input3} ‘
B) INTEZERS AND FLOATS
1. TYPE OF VARIABLE: function type is used to know the type of variable
output:
output:
2 Arthematic operations
output: we can directly use all the arthematic operaters like +,-,*,/
Whereas the bouble backslash and percentage synbol were used to get quocient and reminder
output:
And double star can be used for exponent like below
output
3. Logical operators:
output :
4.Increment:
output:
Instead of this we can also do
output:
Simillarly for multiplication,
output:
5. converting STRING TO INT AND VISEVERSA
output:
Eventough 100 and 200 were intezers placing them between exclamatery marks converts int to string type. So the
result is 100200.
output
By using int function we can convert string into intezers
output
Simillary str function can be used to change int to string.
6 rounding off numbers
output
We can also round it to any decimal places of our convienence
output :
output :