- Python has several build in commands to easy the day to day programming.
- Check here for available built in functions.
- Boolean are handled in python in a bit different way. Normal convention is to use
true
andfalse
for boolean. But in Python we useTrue
andFalse
- In python to represent Null we use
None
. Pay attention to capitalN
finally
block intry
andexcept
work exactly the same asfinally
block in Javaelse
block can be used to perform an action when there is no exception.raise
keyword is use to throw an exception.
- math
- os
- random
- sys
- re
- json
- datetime
input
is the function used to take user input.- The value return by
input
function is string. - You should convert the return from
input
call to whatever you expect as input.
age = input("enter your age:") # enter 30
type(age) # will return string
age = int(age) # Convert it to type which you expect.
type(age) # will return int
- capitalize - Change to upper case the first letter of the string.
- title - Change to upper case first letter of all the words in the string
- upper - Change all chars in the string to upper case
- lower - change all chars in the string to lower case
- count - return the total number of given char in the string. case sensitive.
- find - return the position of a char in the string.
- split - split the string based on the separator provided.
- Tuple is immutable ordered list. Can contain duplicates.
- Tuple elements are access in same fashion as list.
- Tuple are created using parenthesis in place of square brackets as in list.
primes = (3,7,11)
primes[0]
my_list = [1,2,3,45,56]
print(my_list[2:4]) # prints 3, 45
print(my_list[2:]) # prints 3, 45,56
print(my_list[:4]) # prints 1,2,3,45
print(my_list[::1]) # prints 1,2,3,45,56
print(my_list[::2]) # prints 1,3,56
print(my_list[2::2]) # prints 3,56
print(my_list[1:4:2]) # prints 2,45
classmethod
andstaticmethod
behave a bit similarly.- Difference is a class method will take a class reference as argument along with other argument(if needed), where are in
@staticmethod
no argument are mandatory.