You are on page 1of 5

PYTHON

OUTPUT

 Print is used to print a value


 To print a variable use + sign
 You cant print string and int together instead convert the string into int format and
then print the value Ex: (“hi,”+str(a))
…………………………………………………………………………………………………

INPUT

 Syntax for input


a = input(“enter the value”)
 No need of ; to initialize a variable
 Space is important
 If you want to enter 2 value then syntax is
a = input(“enter 2 values”)
b = input()
 For making it easy you can add int in while calling the variables
Ex: a= int(input(enter the value))
If this is done , there is no need to add int every time you type the variable but while
printing the value you need to convert int to str
…………………………………………………………………………………………………

SUM/SUB

 Syntax is
Sum= int(a) + int(b)
Print (“result is” + str(sum))
…………………………………………………………………………………………………
STRING

 To know the type of the variable, type is the function used. syntax :
Print(type(a))
 To print the length od the variable, len is the function used. Syntax
Print(len(a))

 Print(a[5]) # To print the letter in a particular position. Syntax


Print(a[4:6]) # to print the string from 4th position till 6th position
Print(a[-6:-1]) # to print the string from -6th position till -1th position(from backward)
It wont run is it isn’t reverse order
 Print(a.strip()) # removes the space before and after the sentence. We should follow
the same format as syntax else error can be occurred.
 Print(a.lower()) #to print the string in lower case
 Print(a.upper()) #to print the string in upper case
 Print(a.replace(“h”,”j”)) # h will be replaced with j
 Print(a.split(“,”)
…………………………………………………………………………………………………

 Comment is written using #


In order to write multi line comments use “””(triple quotes in the beginning and end)
Ex: “””
Ahycbud
Jnc hdnd
“””
LIST

 We cant change the words or part of a sentence which is already defined-immutable


because it can’t be changed
 But then the sentence is defined in a list is can be changes-mutable because it can be
changed
Ex;
Names=[“what”,”is”,”your”,”name”]
Names[0]= meril
Print(names)
Output
[‘meril’,’is’,’your’,’name’]
 To add more to the list
Names=names+[‘welcome’,’hai’]
 To enter a word by user at the end-append is the function used
Names.append(input(“enter a value”))
CONTROL STATEMENT

 If statement
Syntax:
Num=int(input(“enter the number”))
If num<0 : # : is used instead of {
Print(“entered value is positive”) # the statement after if should come after a gap
which indicates that statement is under the if statement
Elif (num == 0) # python use elif instead of else if
Print(‘value is zero’)
Else
Print(“value is negative”)
 While statement
i=1
while (i<=10) :
print(i)
i=i+1
else :
print (“loop \n finished”) # \n will make the rest of the sentence to be printed in
next line
 For
Syntax
num=int(input("enter the number"))
for i in range(1,num+1):# 1 because num number of *should be printed till
num+1
print(" "*(num-i) + "* "*i) #num-i is used to declare space

this program give a middle triangle shape



for i in range(10,20,4): # the 3rd value determine the gap need to print
next value means all after 10 14 will be printed then 18
print(i)

FUNCTION

 syntax
Dif hey() :
Print (“hello”)
Hey()
 diff between tuple() and list[]
you can add more variables to a list but not into tuple
 if u want to pass lots of arguments then there is something called arbitrary arjument
 valus inside function is local variables outside is global variable
 key word= variables that are initialized inside a function call
 to save in dictonory value={“name”=”meril”, age=”25”}
print(value.get(“age”))
 each file is called modules. To import a function to other module it is possible by
following syntax
syntax:
import file_name
 print(__name__) this Is used to understand frm which file the function was called
from
 from file_name import function_name in this case only the function from that
file will execute
 constructors are used to work automatically without calling the function
web analysis

1. download python from python.org.download


2. in cmt prompt type pip install jupyter notebook
3. in another cmt prompt type install pandas
4. to open jupyter notebook. Type jupyter notebook in cmt prompt-onedrive-
desktop-new-create new folder else 127.0.0.1:8888
5. import json
import pandas as pd
6. file=open(“file_name”)
7. val=file.readlines()
8. json.loads(val[0])[‘timestamp’]
…………………………………………………………………………………
9. m=pd.read_json("interactionstext.json",lines=True)
………………………….
import pandas as pd
import json
import datetime
……………
m=pd.read_json("interactionsnew.json",lines=True)
m.head()
………….
m.isna().sum()
m.shape
data =m.fillna(0)
data['datetime'] = pd.to_datetime(data['timestamp'], unit='ms', errors='coerce')
data[ 'human_readable'] = data['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
…………
hotel_counts = data['hotel'].value_counts()
hotel_counts
…………………………
data.head(10)
…………………………………….
d = data[["customerId" , "human_readable"]].copy()
d

You might also like