All the function learnt this week - Week 3
Individual Characters :
This function is used to get individual characters but is it is better to use the for loop as it will error out if there isn't the same amount of letters.
This form of the example will print it vertical as h
Example - e
msg = "hello world"
print(msg[0])
print(msg[1])
Example -
name = input('Enter your name? ')
for c in name:
print(c.upper())
Upper &Lower Case
This function is used to get individual characters but is it is better to use the for loop as it will error out if there isn't the same amount of letters.
This form of the example will print it vertical as h
Example - e
msg = "hello world"
print(msg[0])
print(msg[1])
Looping
This will loop through the coding until it either finds the answer or input the computer was looking for or is stopped. This function uses for in the variable which is what loops it.Example -
name = input('Enter your name? ')
for c in name:
print(c.upper())
Upper &Lower Case
To print the program in either lower or upper case simply use the variable that you want to be in lower or upper case and add .lower or .upper with brackets.
Example -
print(c.upper()) or print(.lower())
On the same line
When you use for to loop it always print vertically and not on the same line. To print it on the same line we use end=. or output it which is the better way of doing it
Example -
The end= way
for i in "Hello":
print(i, end=" ")
The output way
output = ''
for i in "Hello":
output += i + ' '
output += 'same line'
print(output)
Range
Range is used to break or stop the program from looping again. It is used with the for statement.
Example -
for n in range(5, 9):
print(n)
Which will print the numbers 5,6,7,8
Step by Step
Adding a third argument to range - such as 2 for even numbers.When entering the numbers it range take it as "Print the numbers from 2 up to (but not including) 12, in steps of 2"
Example -
for i in range(2, 12, 3):
print(i)
2
5
8
11
Adding a third argument to range - such as 2 for even numbers.When entering the numbers it range take it as "Print the numbers from 2 up to (but not including) 12, in steps of 2"
Example -
for i in range(2, 12, 3):
print(i)
2
5
8
11
Backwards
To loop it in reverse you simply go to make the third argument a negative
Example -
for i in range(5, 0, -1):
print(i)
5
4
3
2
1
Same Line Continued
To loop it in reverse you simply go to make the third argument a negative
Example -
for i in range(5, 0, -1):
print(i)
5
4
3
2
1
Same Line Continued
When using output in this example it leaves a white space at the end
Example -
output = ""
for i in range(10):
output = output + str(i) + ' '
print(output)
To fix this we can use either rstrip or string slicing.
Example -
print(output[:-1])
or
print(output.rstrip())
Maths with looping
To do maths in looping we can use for and a variable of 0.
Example -
result = 0
for i in range(100):
result = result + i
print(result)
Changing integer into float
If we did not convert them when dividing the answer will always be in a float even when not necessary. Such as 10/ 2 would = 5.0
Example -
i = 10
print(i)
f = float(i)
print(f)
10
10.0
Changing float into integer
To do this we simply need to use the int function.
Example -
i = int(5.0)
print(i)
5
**Note that if the decimal is like 4.5 it wont round it up or down it will simply chop it off
Example -
print(int(4.8))
4
Calculating remainders
To calculate the remainder we use the percentage sign like this remainder = 10 % 3
print(remainder)
Example - In a program
answer = int(290327/36)
remainder = 290327 % 36
print(answer)
print(remainder)
8064
23
Example -
output = ""
for i in range(10):
output = output + str(i) + ' '
print(output)
To fix this we can use either rstrip or string slicing.
Example -
print(output[:-1])
or
print(output.rstrip())
Maths with looping
To do maths in looping we can use for and a variable of 0.
Example -
result = 0
for i in range(100):
result = result + i
print(result)
Changing integer into float
If we did not convert them when dividing the answer will always be in a float even when not necessary. Such as 10/ 2 would = 5.0
Example -
i = 10
print(i)
f = float(i)
print(f)
10
10.0
Changing float into integer
To do this we simply need to use the int function.
Example -
i = int(5.0)
print(i)
5
**Note that if the decimal is like 4.5 it wont round it up or down it will simply chop it off
Example -
print(int(4.8))
4
Calculating remainders
To calculate the remainder we use the percentage sign like this remainder = 10 % 3
print(remainder)
Example - In a program
answer = int(290327/36)
remainder = 290327 % 36
print(answer)
print(remainder)
8064
23
Import
Import is used to import math functions for python
Example -
import math
print(math.sqrt(4))
Functions & Other Math
Round a number:
print(round(4.6))
Square a number:
import math
print(math.sqrt(4))
Pi:
import math
print(math.pi)
Advance String Looping
Advance string will show a index of the letters in the loop
Example -
y = 'hello'
for i in range(len(y)):
line = "Letter " + str(i) + " is " + y[i]
print(line)
Letter 0 is h
Letter 1 is e
Letter 2 is l
Letter 3 is l
Letter 4 is o
Checking the Start and End Of a String
This checks whether the string you have entered is at the start or the end of the input. s.startswith() or s.endswith()
Example -
s = "hello world"
print(s.startswith('hello'))
print(s.endswith('rld'))
True
True
String Methods
Removing white space:
Example -
s = " abc "
print(s.strip())
print(s.lstrip())
print(s.rstrip())
abc
abc
abc
Finding the Index of a Character:
Example -
s = "hello world"
print(s.find('w'))
print(s.find('x'))
6
-1
** If the character is not in the string it will come up as -1
Chopping up a string
This means you can access parts of the string rather then the whole thing
Example -
x = 'hello world'
print(x[0:5])
print(x[6:11])
hello
world
Short Cut :
x = 'hello world'
print(x[:5])
print(x[6:])
hello
world
If you want to replace a part of the string by slicing it you will have to create a new string.
Example -
x = 'hello world'
x = 'X' + x[1:]
print(x)
'Xello world'
No comments:
Post a Comment