You are on page 1of 20

Python Basics

variable:
irshath = ‘hello world’

printing in python:
print(‘hello world’)

getting input in python:


input(‘Enter your name’)
note:
the default datatype in input function is string.
if you are going to enter a string
input(“Enter your name”)
if you are going to enter integer(number)
int(input(‘Enter your phone number’))
if you are going to enter a bool (i.e) True / False
bool(input(“True/False”))

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

to get a single letter of a string

just type the index of the string to get the output

to get from a particular letter to particular letter from a string


name = irshath
print(name[0:3])
output:
irsh

string concatenation:
you can add strings in python
eg:
first_name = "mohammed "
last_name = "irshath"
print(first_name + last_name + "is a coder")

you can insert a variable in between a string

first_name = "mohammed "
last_name = "irshath"
print(f"{first_name} {last_name} is a coder")

you should use a keyword f in the starting to do that


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

Examples of all the above Methods


arithmetic operators:
10 + 10 = 20 addition
10 – 10 = 0 subtraction
10 * 10 = 100 multiplication
10 / 3 = 3.333 division
10 // 3 = 3 it is also division but it will omit the decimal numbers and returns always
the whole number
10 % 3 = 1 returns the remainder of the division
10 ** 3 = 1000 square root (i.e) it is the square value 10 X 10 X 10

short form for add sub mul div in modifying a variable:


Long code
x = 10
x=x+3
short form
x+=3
x-=3
x*=3
x/=3
like this you can shorter the code
this shows what operations will be execute first(first priority)
first 1. parenthesis()
2.exponentiation 2**3
3. multiplication or division
4. addition or subtraction

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:

always use i+=1 when using while statement


while also has else statement. else will be executed when the loop completely
executed.
break is used to exit the loop
for loop:

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

you can import modules syntax given below


import app
python file name
you can also import a specific function or method in python
from app import irshathFunc
packages or directories:
packages or directories are called folders where the python files are available

if you want to create a new folder and paste all the python files you should follow
the below rule

step1: create a new folder


step2: after creating open that folder and do the following
__init__.py
you should type this python file as exactly as this to make your folder work
it is like showing python that it is a folder for creating python files
step3: now you can create your python files in the new folder

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

folder name python file name

You might also like