-
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.
-
Python can be used for:
- Web Development (Server-Side)
- Software Development
- Mathematics
- System Scripting
- Variables are containers for storing data values.
- Unlike other programming languages, Python has no command for declaring a variable. eg:
x = 5
y = "Hello, World!"
print(x)
print(y)Output:
5
Hello, World!- Variable Names:
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (age, Age and AGE are three different variables).
- The variable name must not contain any special characters like !, @, #, $, % etc.
- A variable name cannot be any of the Python keywords
- Comments can be used to explain Python code.
- Comments can be used to make the code more readable. eg:
# This is a comment
print("Hello, World!")Output:
Hello, World!- Multi Line Comments
""" This is a comment
written in
more than just one line """
print("Hello, World!")Output:
Hello, World!- To combine both text and a variable, Python uses the + character: eg:
x = "awesome"
print("Python is " + x)Output:
Python is awesome- You can also use the + character to add a variable to another variable: eg:
x = "Python is "
y = "awesome"
z = x + y
print(z)Output:
Python is awesome-
In programming, data type is an important concept.
-
Variables can store data of different types, and different types can do different things.
-
Python has the following data types built-in by default, in these categories:
- Text Type:
str - Numeric Types:
int,float,complex - Sequence Types:
list,tuple,range - Mapping Type:
dict - Set Types:
set,frozenset - Boolean Type:
bool - Binary Types:
bytes,bytearray,memoryview - None Type:
NoneType
- Text Type:
-
Setting data types:
x = "Hello World" # str
x = 20 # int
x = 20.5 # float
x = 1j # complex
x = ["apple", "banana", "cherry"] # list
x = ("apple", "banana", "cherry") # tuple
x = range(6) # range
x = {"name" : "John", "age" : 36} # dict
x = {"apple", "banana", "cherry"} # set
x = frozenset({"apple", "banana", "cherry"}) # frozenset
x = True # bool
x = b"Hello" # bytes
x = bytearray(5) # bytearray
x = memoryview(bytes(5)) # memoryview
x = None # NoneType- Setting the Specific Data Type:
x = str("Hello World") # str
x = int(20) # int
x = float(20.5) # float
x = complex(1j) # complex
x = list(("apple", "banana", "cherry")) # list
x = tuple(("apple", "banana", "cherry")) # tuple
x = range(6) # range
x = dict(name="John", age=36) # dict
x = set(("apple", "banana", "cherry")) # set
x = frozenset(("apple", "banana", "cherry")) # frozenset
x = bool(5) # bool
x = bytes(5) # bytes
x = bytearray(5) # bytearray
x = memoryview(bytes(5)) # memoryview-
There are three numeric types in Python:
- int
- Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
- float
- Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
- complex
- Complex numbers are written with a "j" as the imaginary part.
- int
-
Variables of numeric types are created when you assign a value to them: eg:
x = 1 # int y = 2.8 # float z = 1j # complex
-
Note: You cannot convert complext numbers into another number type.
-
Random Number:
import random
print(random.randrange(1, 10))Output:
4-
There may be times when you want to specify a type on to a variable. This can be done with casting.
-
Casting in python is therefore done using constructor functions:
int()float()str()
-
Integers:
x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3
-
Floats:
x = float(1) # x will be 1.0 y = float(2.8) # y will be 2.8 z = float("3") # z will be 3.0 w = float("4.2") # w will be 4.2
-
Strings:
x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0'
- Strings in python are surrounded by either single quotation marks, or double quotation marks.
stris the data type for strings in python.- You can display a string literal with the
print()function:
print("Hello")
print('Hello')Output:
Hello
Hello- Assigning a string to a variable:
a = "Hello"
print(a)Output:
Hello- Multiline Strings:
a = """This is a multiline string,
come back, I still miss you,
but whocares,
hahaha"""
print(a)Output:
This is a multiline string,
come back, I still miss you,
but whocares,
hahaha- Strings are Arrays:
a = "Hello, World!"
print(a[1])Output:
e- Slicing:
b = "Hello, World!"
print(b[2:5])Output:
llo- Negative Indexing:
b = "Hello, World!"
print(b[-5:-2])Output:
orl- String Length:
a = "Hello, World!"
print(len(a))Output:
13-
String Methods:
strip()- Removes any whitespace from the beginning or the endlower()- Returns the string in lower caseupper()- Returns the string in upper casereplace()- Replaces a string with another stringsplit()- Splits the string into substrings if it finds instances of the separatorcapitalize()- Converts the first character to upper casecasefold()- Converts string into lower casecenter()- Returns a centered stringcount()- Returns the number of times a specified value occurs in a stringencode()- Returns an encoded version of the stringendswith()- Returns true if the string ends with the specified valueexpandtabs()- Sets the tab size of the stringfind()- Searches the string for a specified value and returns the position of where it was foundformat()- Formats specified values in a stringindex()- Searches the string for a specified value and returns the position of where it was foundisalnum()- Returns True if all characters in the string are alphanumericisalpha()- Returns True if all characters in the string are in the alphabetisdecimal()- Returns True if all characters in the string are decimalsisdigit()- Returns True if all characters in the string are digitsisidentifier()- Returns True if the string is an identifierislower()- Returns True if all characters in the string are lower caseisnumeric()- Returns True if all characters in the string are numericisprintable()- Returns True if all characters in the string are printableisspace()- Returns True if all characters in the string are whitespacesistitle()- Returns True if the string follows the rules of a titleisupper()- Returns True if all characters in the string are upper casejoin()- Joins the elements of an iterable to the end of the stringljust()- Returns a left justified version of the stringlstrip()- Returns a left trim version of the stringmaketrans()- Returns a translation table to be used in translationspartition()- Returns a tuple where the string is parted into three partsrfind()- Searches the string for a specified value and returns the last position of where it was foundrindex()- Searches the string for a specified value and returns the last position of where it was foundrjust()- Returns a right justified version of the stringrpartition()- Returns a tuple where the string is parted into three partsrsplit()- Splits the string at the specified separator, and returns a listrstrip()- Returns a right trim version of the stringsplitlines()- Splits the string at line breaks and returns a liststartswith()- Returns true if the string starts with the specified valueswapcase()- Swaps cases, lower case becomes upper case and vice versatitle()- Converts the first character of each word to upper casetranslate()- Returns a translated stringzfill()- Fills the string with a specified number of 0 values at the beginning
-
Check String:
isalnum()- Returns True if all characters in the string are alphanumericisalpha()- Returns True if all characters in the string are in the alphabetisdecimal()- Returns True if all characters in the string are decimalsisdigit()- Returns True if all characters in the string are digitsisidentifier()- Returns True if the string is an identifierislower()- Returns True if all characters in the string are lower caseisnumeric()- Returns True if all characters in the string are numericisprintable()- Returns True if all characters in the string are printableisspace()- Returns True if all characters in the string are whitespacesistitle()- Returns True if the string follows the rules of a titleisupper()- Returns True if all characters in the string are upper case
-
String Format:
format()- Formats specified values in a stringcapitalize()- Converts the first character to upper casecasefold()- Converts string into lower casecenter()- Returns a centered stringcount()- Returns the number of times a specified value occurs in a stringencode()- Returns an encoded version of the stringendswith()- Returns true if the string ends with the specified valueexpandtabs()- Sets the tab size of the stringfind()- Searches the string for a specified value and returns the position of where it was foundindex()- Searches the string for a specified value and returns the position of where it was foundjoin()- Joins the elements of an iterable to the end of the stringljust()- Returns a left justified version of the stringlower()- Converts a string into lower caselstrip()- Returns a left trim version of the stringmaketrans()- Returns a translation table to be used in translationspartition()- Returns a tuple where the string is parted into three partsreplace()- Returns a string where a specified value is replaced with a specified valuerfind()- Searches the string for a specified value and returns the last position of where it was foundrindex()- Searches the string for a specified value and returns the last position of where it was foundrjust()- Returns a right justified version of the stringrpartition()- Returns a tuple where the string is parted into three partsrsplit()- Splits the string at the specified separator, and returns a listrstrip()- Returns a right trim version of the stringsplit()- Splits the string at the specified separator, and returns a listsplitlines()- Splits the string at line breaks and returns a liststartswith()- Returns true if the string starts with the specified valuestrip()- Returns a trimmed version of the stringswapcase()- Swaps cases, lower case becomes upper case and vice versatitle()- Converts the first character of each word to upper case
-
Check if a certain phrase or character is present in a string
txt = "God is watching." x = "NOT" in txt print(x)
Output:
False -
Check if a certain phrase or character is NOT present in a string
txt = "Saturn is my favourite planet." x = "Earth" not in txt print(x)
Output:
True -
String Concatenation:
a = "Hello" b = "World" c = a + b print(c)
Output:
HelloWorld -
String Format:
age = 36 txt = "My name is John, and I am {}" print(txt.format(age))
Output:
My name is John, and I am 36
-
Escape Character:
- To insert characters that are illegal in a string, use an escape character.
- An escape character is a backslash
\followed by the character you want to insert. - An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
txt = "We are the so-called \"Vikings\" from the north." print(txt)
Output:
We are the so-called "Vikings" from the north.
- Booleans represent one of two values:
TrueorFalse. - In programming you often need to know if an expression is
TrueorFalse.
-
Operators are used to perform operations on variables and values.
-
Python divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
-
Arithmetic Operators:
+- Addition-- Subtraction*- Multiplication/- Division%- Modulus**- Exponentiation//- Floor division
-
Assignment Operators:
=- x = 5+=- x += 3-=- x -= 3*=- x *= 3/=- x /= 3%=- x %= 3//=- x //= 3**=- x **= 3&=- x &= 3|=- x |= 3^=- x ^= 3>>=- x >>= 3<<=- x <<= 3
-
Comparison Operators:
==- Equal!=- Not equal>- Greater than<- Less than>=- Greater than or equal to<=- Less than or equal to
-
Logical Operators:
and- Returns True if both statements are trueor- Returns True if one of the statements is truenot- Reverse the result, returns False if the result is true
-
Identity Operators:
is- Returns True if both variables are the same objectis not- Returns True if both variables are not the same object
-
Membership Operators:
in- Returns True if a sequence with the specified value is present in the objectnot in- Returns True if a sequence with the specified value is not present in the object
-
Bitwise Operators:
&- AND|- OR^- XOR~- NOT<<- Zero fill left shift>>- Signed right shift
- Lists are used to store multiple items in a single variable.
- Lists are created using square brackets:
thislist = ["apple", "banana", "cherry"]
print(thislist)Output:
['apple', 'banana', 'cherry']-
List Items:
- List items are ordered, changeable, and allow duplicate values.
- List items are indexed, the first item has index [0], the second item has index [1] etc.
-
List Items - Data Types:
- List items can be of any data type:
list1 = ["apple", "banana", "cherry"] list2 = [1, 5, 7, 9, 3] list3 = [True, False, False]
-
A list can contain different data types:
list1 = ["apple", "banana", "cherry"] list2 = [1, 5, 7, 9, 3] list3 = [True, False, False] list4 = ["apple", 5, True, "banana", "cherry"]
-
type():
- From Python's perspective, lists are defined as objects with the data type 'list':
mylist = ["apple", "banana", "cherry"] print(type(mylist))
Output:
<class 'list'>
- Tuples are used to store multiple items in a single variable.
- A tuple is a collection which is ordered and unchangeable.
- Tuples are written with round brackets.
thistuple = ("apple", "banana", "cherry")
print(thistuple)Output:
('apple', 'banana', 'cherry')-
Tuple Items:
- Tuple items are ordered, unchangeable, and allow duplicate values.
- Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
-
Tuple Items - Data Types:
- Tuple items can be of any data type:
tuple1 = ("apple", "banana", "cherry") tuple2 = (1, 5, 7, 9, 3) tuple3 = (True, False, False)
-
A tuple can contain different data types:
tuple1 = ("apple", "banana", "cherry") tuple2 = (1, 5, 7, 9, 3) tuple3 = (True, False, False) tuple4 = ("apple", 5, True, "banana", "cherry")
-
type(): - From Python's perspective, tuples are defined as objects with the data type 'tuple':
mytuple = ("apple", "banana", "cherry") print(type(mytuple))
Output:
<class 'tuple'>
- Sets are used to store multiple items in a single variable.
- A set is a collection which is both unordered and unindexed.
- Sets are written with curly brackets.
thisset = {"apple", "banana", "cherry"}
print(thisset)Output:
{'apple', 'banana', 'cherry'}-
Set Items:
- Set items are unordered, unchangeable, and do not allow duplicate values.
- Set items are indexed, the first item has index [0], the second item has index [1] etc.
-
Set Items - Data Types:
- Set items can be of any data type:
set1 = {"apple", "banana", "cherry"} set2 = {1, 5, 7, 9, 3} set3 = {True, False, False}
-
A set can contain different data types:
set1 = {"apple", "banana", "cherry"} set2 = {1, 5, 7, 9, 3} set3 = {True, False, False} set4 = {"apple", 5, True, "banana", "cherry"}
-
type(): - From Python's perspective, sets are defined as objects with the data type 'set':
myset = {"apple", "banana", "cherry"} print(type(myset))
Output:
<class 'set'>
- Dictionaries are used to store data values in key:value pairs.
- A dictionary is a collection which is ordered*, changeable and does not allow duplicates.
- Dictionaries are written with curly brackets, and have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}-
Dictionary Items:
- Dictionary items are ordered, changeable, and does not allow duplicates.
- Dictionary items are indexed, the first item has index [0], the second item has index [1] etc.
-
Dictionary Items - Data Types:
- Dictionary items can be of any data type:
thisdict = { "brand": "Ford", "electric": False, "year": 1964, "colors": ["red", "white", "blue"] }
-
type(): - From Python's perspective, dictionaries are defined as objects with the data type 'dict':
mydict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(type(mydict))
Output:
<class 'dict'>
-
Python supports the usual logical conditions from mathematics:
- Equals:
a == b - Not Equals:
a != b - Less than:
a < b - Less than or equal to:
a <= b - Greater than:
a > b - Greater than or equal to:
a >= b
- Equals:
a = 33
b = 200
if b > a:
print("b is greater than a")Output:
b is greater than a-
Elif:
- The
elifkeyword is pythons way of saying "if the previous conditions were not true, then try this condition".
- The
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")Output:
a and b are equal-
Else:
- The
elsekeyword catches anything which isn't caught by the preceding conditions.
- The
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")Output:
a is greater than b-
Short Hand If:
- If you have only one statement to execute, you can put it on the same line as the
ifstatement.
- If you have only one statement to execute, you can put it on the same line as the
if a > b: print("a is greater than b")Output:
a is greater than b-
Short Hand If...Else:
- If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.
a = 2
b = 330
print("A") if a > b else print("B")Output:
B-
And:
- The
andkeyword is a logical operator, and is used to combine conditional statements.
- The
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")Output:
Both conditions are True-
Or:
- The
orkeyword is a logical operator, and is used to combine conditional statements.
- The
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")Output:
At least one of the conditions is True-
Nested If:
- You can have
ifstatements insideifstatements, this is called nestedifstatements.
- You can have
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")Output:
Above ten,
and also above 20!- With the
whileloop we can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1Output:
1
2
3
4
5-
The
breakStatement:- With the
breakstatement we can stop the loop even if the while condition is true.
- With the
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1Output:
1
2
3-
The
continueStatement:- With the
continuestatement we can stop the current iteration, and continue with the next.
- With the
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)Output:
1
2
4
5
6-
The
elseStatement:- With the
elsestatement we can run a block of code once when the condition no longer is true.
- With the
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")Output:
1
2
3
4
5
i is no longer less than 6- A
forloop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)Output:
apple
banana
cherry- Looping Through a String:
for x in "banana":
print(x)Output:
b
a
n
a
n
a-
The
breakStatement:- With the
breakstatement we can stop the loop before it has looped through all the items.
- With the
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
breakOutput:
apple
banana-
The
continueStatement:- With the
continuestatement we can stop the current iteration of the loop, and continue with the next.
- With the
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)Output:
apple
cherry-
The
range()Function:- To loop through a set of code a specified number of times, we can use the
range()function.
- To loop through a set of code a specified number of times, we can use the
for x in range(6):
print(x)Output:
0
1
2
3
4
5-
The
range()Function:- The
range()function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter:range(2, 6), which means values from 2 to 6 (but not including 6):
- The
for x in range(2, 6):
print(x)Output:
2
3
4
5-
The
range()Function:- The
range()function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter:range(2, 30, 3):
- The
for x in range(2, 30, 3):
print(x)Output:
2
5
8
11
14
17
20
23
26
29-
Else in For Loop:
- The
elsekeyword in aforloop specifies a block of code to be executed when the loop is finished.
- The
for x in range(6):
print(x)
else:
print("Finally finished!")Output:
0
1
2
3
4
5
Finally finished!-
Nested Loops:
- A nested loop is a loop inside a loop.
- The "inner loop" will be executed one time for each iteration of the "outer loop".
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)Output:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result.
def my_function():
print("Hello from a function")
my_function()Output:
Hello from a function-
Arguments:
- Information can be passed into functions as arguments.
def my_function(fname):
print(fname + " Kumar")
my_function("Rahul")
my_function("Rohit")
my_function("Raj")Output:
Rahul Kumar
Rohit Kumar
Raj Kumar-
Number of Arguments:
- By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Rahul", "Kumar")Output:
Rahul Kumar-
Arbitrary Arguments,
*args:- If you do not know how many arguments that will be passed into your function, add a
*before the parameter name in the function definition.
- If you do not know how many arguments that will be passed into your function, add a
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")Output:
The youngest child is Linus-
Keyword Arguments:
- You can also send arguments with the key = value syntax.
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")Output:
The youngest child is Linus-
Arbitrary Keyword Arguments,
**kwargs:- If you do not know how many keyword arguments that will be passed into your function, add two asterisk:
**before the parameter name in the function definition.
- If you do not know how many keyword arguments that will be passed into your function, add two asterisk:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Aditya", lname = "Raj")Output:
His last name is Raj-
Default Parameter Value:
- The following example shows how to use a default parameter value.
def my_function(country = "India"):
print("I am from " + country)
my_function("Sweden")
my_function("Norway")
my_function()
my_function("Brazil")Output:
I am from Sweden
I am from Norway
I am from India
I am from Brazil-
Passing a List as an Argument:
- You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)Output:
apple
banana
cherry-
Return Values:
- To let a function return a value, use the
returnstatement.
- To let a function return a value, use the
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))Output:
15
25
45-
The
passStatement:- function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the
passstatement to avoid getting an error.
- function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the
def myfunction():
pass- A lambda function is a small anonymous function.
- A lambda function can take any number of arguments, but can only have one expression.
x = lambda a : a + 10
print(x(5))Output:
15- Lambda functions can take any number of arguments:
x = lambda a, b : a * b
print(x(5, 6))Output:
30x = lambda a, b, c : a + b + c
print(x(5, 6, 2))Output:
13-
Why Use Lambda Functions?
- The power of lambda is better shown when you use them as an anonymous function inside another function.
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))Output:
22
33- Arrays are used to store multiple values in one single variable.
cars = ["Ford", "Volvo", "BMW"]-
Access the Elements of an Array:
- You refer to an array element by referring to the index number.
x = cars[0]- Modify the value of the first array item:
cars[0] = "Toyota"-
The Length of an Array:
- Use the
len()method to return the length of an array (the number of elements in an array).
- Use the
x = len(cars)-
Looping Array Elements:
- You can use the
forin loop to loop through all the elements of an array.
- You can use the
for x in cars:
print(x)-
Adding Array Elements:
- You can use the
append()method to add an element to an array.
- You can use the
cars.append("Honda")-
Removing Array Elements:
- You can use the
pop()method to remove an element from the array.
- You can use the
cars.pop(1)-
Array Methods:
append()- Adds an element at the end of the listclear()- Removes all the elements from the listcopy()- Returns a copy of the listcount()- Returns the number of elements with the specified valueextend()- Add the elements of a list (or any iterable), to the end of the current listindex()- Returns the index of the first element with the specified valueinsert()- Adds an element at the specified positionpop()- Removes the element at the specified positionremove()- Removes the first item with the specified valuereverse()- Reverses the order of the listsort()- Sorts the list
-
Python is an object oriented programming language.
-
Almost everything in Python is an object, with its properties and methods.
-
A Class is like an object constructor, or a "blueprint" for creating objects.
class MyClass:
x = 5-
Create an Object:
- Now we can use the class named
MyClassto create objects.
- Now we can use the class named
p1 = MyClass()
print(p1.x)Output:
5-
The
__init__()Function:- The examples above are classes and objects in their simplest form, and are not really useful in real life applications.
- To understand the meaning of classes we have to understand the built-in
__init__()function. - All classes have a function called
__init__(), which is always executed when the class is being initiated. - Use the
__init__()function to assign values to object properties, or other operations that are necessary to do when the object is being created.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)Output:
John
36-
Object Methods:
- Objects can also contain methods. Methods in objects are functions that belong to the object.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()Output:
Hello my name is John-
The
selfParameter:- The
selfparameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
- The
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()Output:
Hello my name is John-
Modify Object Properties:
- You can modify properties on objects like this:
p1.age = 40-
Delete Object Properties:
- You can delete properties on objects by using the
delkeyword:
- You can delete properties on objects by using the
del p1.age-
Delete Objects:
- You can delete objects by using the
delkeyword:
- You can delete objects by using the
del p1-
Inheritance allows us to define a class that inherits all the methods and properties from another class.
-
Parent Class:
- The class being inherited from is called the parent class.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()Output:
John Doe-
Child Class:
- The class that inherits from another class is called the child class.
class Student(Person):
pass-
Use the
super()Function:- The
super()function is used to give access to methods and properties of a parent or sibling class.
- The
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
x = Student("Aditya", "Raj")
x.printname()Output:
Aditya Raj-
Add Properties:
- Add a property called
graduationyearto theStudentclass.
- Add a property called
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Aditya", "Raj", 2027)-
Add Methods:
- Add a method called
welcometo theStudentclass.
- Add a method called
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student- Python allows for user input.
username = input("Enter username:")
print("Username is: " + username)Output:
Enter username: Aditya
Username is: Aditya