Python The Guide
Tags: python, datascience, machinelearning, programming
Published: 2022-06-26 17:54:02

>>> print("Hello World")
Hello World
>>> 2*3
6
>>> 2**3
8
>>> 3/4
0.75
>>> a=23
>>> b=90
>>> a-b
-67
>>> a+b
113
>>> a**b
359339733669351605185101286718740311628342797555834676291480724848488155215018843334859391939330168929445333462245499312849
>>> s = "Engineering"
>>> s[:7]
'Enginee'
>>> s[1:]
'ngineering'
>>> len(s)
11
>>> type(s)
<class 'str'>
>>> max(s)
'r'
>>> min(s)
'E'
>>> b=input("Enter string")
Enter stringCollege
>>> b
'College'
>>> s+b
'EngineeringCollege'
>>> s+" "+b
'Engineering College'
>>> c = s+" "+b
>>> c
'Engineering College'
>>> c.capitalize()
'Engineering college'
>>> c.lower()
'engineering college'
>>> c.upper()
'ENGINEERING COLLEGE'
>>> "my name is %s from %s"%("Soham","Arambagh")
'my name is Soham from Arambagh'
>>> str="arun C machine hack"
>>> str.split()
['arun', 'C', 'machine', 'hack']
>>> str[1]
'r'
>>> c.title()
'Engineering College'
>>> str[2:]
'un C machine hack'
>>> str.length()
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    str.length()
AttributeError: 'str' object has no attribute 'length'
>>> len(str)
19
>>> str.center(13)
'arun C machine hack'
>>> str.count("a")
3
>>> str.find("hack)
	 
SyntaxError: EOL while scanning string literal
>>> str.find("hack")
15
>>> str.replace("hack","Don't hack")
"arun C machine Don't hack"

# 3) Accept only vowel containing string

def check(str):
    str = str.lower()
    vowel = set("aeiou")  #Convert string into set of characters
    str1 = set({})     # set function use to convert dictionary to set

    for char in str:
        if char in vowel:
            str1.add(char)
        else:
            pass
    if len(str1)==len(vowel):
        print("string is accepted \n")
    else:
        print("Not accepted\n")       

txt = input("Enter any string :-) \n")
check(txt)

# 9) Programme for reverse number
def recursive(txt):
    
    rem =0
    
    while (txt>0):
       rem = (rem*10) +(txt %10)
       txt = txt//10  #used for quotient

    return rem

txt = int(input("Enter the number\n"))

po = recursive(txt)
print("Reverse number is: ",po)

# 10) sum of digits of a number
def squar(ni):
    sum1 =0
    while(ni !=0):
        sum1 = sum1 +(ni%10)
        ni = ni//10
    return print("Sum of the digits of number is",sum1)

n1 = int(input("Enter the number for addition of digits\n"))
squar(n1)

#11) Sum of cube of digits in a number

def cube(n2):
  
    sum3 =0
    while(n2 !=0):
        con = n2%10
        sum2 =con*con*con
        sum3 = sum3 + sum2
        n2 = n2//10

    return sum3
n2 =int(input("Enter the number for sum of cube\n"))
print("Sum is:",cube(n2))

# 4) Maximum occurring character in string
size = 256
def maxchar(str):
    arr = [0] * size

    max = -1;
    c =''

    for i in str:
        arr[ord(i)] += 1
    
    for i in str:
        if arr[ord(i)]>max:
            max = arr[ord(i)]
            c = i

    return c

st ="engineering icon english"
print(maxchar(st))



# 5) String consist special character or not

import re

def consistchar(str):

    reg ="[^a-zA-Z0-9]+"

    q = re.compile(reg)

    if(len(str)==0):
        print("NO")
        return

    if(re.search(q,str)):
        print("Yes")
    else:
        print("NO")

str ="er@1wed"
print("String consist of ",consistchar(str)) 

# 7)Length of string

str3 =input("Enter any string you want\n")
print("length of the string:",len(str3)) 

# 8) String Contain number or not

def firstnumber(str4):
    c=0
    for i in str4:
        if ord(i)>=48  and ord(i)<=57:
            # print("String contain number")
            # return
            c = c+1
        else:
            #print("String not contain number")
            c=0
       
    if c>=1:
        print("contain number")
        return
    else:
        print("not contain number")

firstnumber("ho")

# Generating  random string until given string not created

import random
import string
import time

possiblechar = string.ascii_lowercase +string.ascii_uppercase+string.digits+'., !?:;'

st=input("Enter the string\n")

#Generating A random String(use to generate password)

atthis =''.join(random.choice(possiblechar)for i in range(len(st)))

atnext =''

complete = False
itr =0

while complete == False:
    print(atthis)

    atnext=''
    complete = True

    #Fix the index
    for i in range(len(st)):
        if atthis[i] != st[i]:
            complete =False
            atnext += random.choice(possiblechar)
        else:
            atnext += st[i]

    itr +=1
    atthis = atnext
    time.sleep(0.1)

print("Target match after ",itr )



OUTPUT:

Enter any string :-) 
aeiuou
string is accepted 

Enter the number
342
Reverse number is:  243
Enter the number for addition of digits
6795
Sum of the digits of number is 27
Enter the number for sum of cube
123
Sum is: 36
n
Yes
String consist of  None
Enter any string you want
Engineering
length of the string: 11
not contain number
Enter the string
soham
;rh?m
……….
……….
sYham
sCham
s;ham
soham
Target match after  312


//MATPLOLIB
import matplotlib.pyplot as mp

x =[2,3,7,8]
y =[4,8,10,15]

mp.plot(x,y,'#000',linewidth=3,linestyle='dotted',marker='o')
mp.show()
 
import matplotlib.pyplot as mp

x=[3,4,5,6]
y =[2,8,7,9]
mp.xlabel("X")
mp.ylabel("Y")
mp.bar(x,y,color=['r','b','y','g'])
mp.show()
 

Pie CHART
import matplotlib.pyplot as plt
w =[200,100,150,34,250]
d =['Math','Physics','Eco','Computer Sc','Python']
plt.pie(w,labels=d,autopct="%3d%%",shadow=True)
plt.show()
 
import matplotlib.pyplot as plt
a =['Ram','Soham','Madhav','Shobho']
b =[3,6,4,2]
plt.title("marks of student")
plt.bar(a,b)
plt.show()
 
import matplotlib.pyplot as plt
a=[2,3,4,5]
b =[7,8,5,6]
plt.xlim(1,3)
plt.ylim(-1,2)
plt.bar(a,b)
plt.show()