Python
Python
variable:
irshath = ‘hello world’
printing in python:
print(‘hello world’)
type function:
type function is used to get the datatype of a variable. it will tell you what datatype
the variable is like int, string, bool , etc..
eg:
number = 10;
print(type(number))
output: <class int>
strings:
you can use either “” or ‘’ for strings
there is another way to define a string
is ‘’’
eg:
print(‘irshath’)
print(“irshath”)
print(‘’’Hello I’m irshath ‘’’)
you can use ‘’’ triple times to define a string
string concatenation:
you can add strings in python
eg:
first_name = "mohammed "
last_name = "irshath"
print(first_name + last_name + "is a coder")
first_name = "mohammed "
last_name = "irshath"
print(f"{first_name} {last_name} is a coder")
String methods:
there are many many string methods are there in python. some of them are given
below.
name = "irshath"
print(len(name)) – used to check total no of letters
print(name.upper()) – changes to UPPERCASE
print(name.lower()) -changes to lowercase
print(name.find("i")) – finds and prints the index of the string which you enter in the
parameter
print(name.replace("irshath", "arshath")) – replaces the string
finding = "irshath" in name
print(finding) – checks whether the string is available or not. if available it prints
True, if not availabe prints False
numeric functions(methods)
there are lot of functions to perform numeric operations in python. you can google it
and refer them. some of them are given below
import math
number = 2.5
print(math.ceil(number))
print(math.floor(number))
output:
3
2
ceil rounds the decimal number to whole number which increases in greater format
floor does the same but it will decrease
if statement:
weather = input("how is the climate today: ")
hot = "hot"
cold = "cold"
if weather == cold:
print("It's a cold day")
print("Wear warm clothes")
elif weather == hot:
print("It's a hot day")
print("Drink plenty of water")
else:
print("It's a lovely day")
logical operator:
and – executes when both element or variable is true
or – executes if one of the element is true
not – reverse the answer if you use not operator, if the element is true it will make it
false and vice versa
eg:
high_income = True
good_credit = False
if high_income or good_credit:
print("Eligible for loan")
else:
print("Not Eligible")
Comparison operator:
>
<
==
>=
<=
!=
etc..
Some Example:
import math
print("Weight Converter")
weight = input("Enter your weight: ")
typeOfWeight = input("Enter Pounds or Kilogram: ")
pounds = "pounds"
kilo = "kilogram"
toPounds = math.ceil(int(weight) * 2.20)
toKilo = math.ceil(int(weight) / 2.20)
if typeOfWeight == kilo:
print(f"You are {toPounds} Pounds")
elif typeOfWeight == pounds:
print(f"You are {toKilo} kilos")
while loop:
syntax:
while condition:
print()
i++
example:
# guessing game
correctGuess = 7
i = 0
while i < 3:
guess = int(input("Guess: "))
i += 1
if guess == correctGuess:
print("You guessed it right")
break
else:
print("Try again")
else:
print("Sorry you're failed")
Note:
List(array) in python
syntax:
list = [1, 2, 3, 4]
list methods:
there are lots of list methods in python just google them
eg:
append(), clear(), push()
just a code to remove repeated number
tuple:
it is same like list but you cannot modify tuple. list can be modified but tuple cant
syntax:
names = (irshath, arshath, umar)
dictionaries:
functions:
exception handling:
comments:
#this is a comment
Class:
classes are like objects . it is very important in oop(object oriented programming)
Syntax:
class IrshathClass:
..........
Note:
the first letter of class name should be capital eg: class Arshath:
after creating class
we should create a variable and type the object eg: ClassName()
constructor:
use
def __init__(self):
to create a constructor
inheritance:
inheritance is called reusability. reusing the code from the parent class is called
inheritance
you can reuse the code used in a class without copying. you just refer the name of
the class to the new class to reuse them. these type of reusing is called inheritance
modules:
modules are python files (i.e) app.py
if you want to create a new folder and paste all the python files you should follow
the below rule
if you want to import a python file which is located in a different folder you can do
that by doing the following
import pythonFiles.app