0% found this document useful (0 votes)
3 views

Basic of Phython

The document provides a comprehensive overview of Python programming basics, including code examples for printing, string manipulation, arithmetic operations, and various types of operators. It also covers functions, error handling, and demonstrates the use of loops and conditionals. Additionally, it explains different types of functions and their parameters, as well as common errors encountered in Python programming.

Uploaded by

pranshavji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Basic of Phython

The document provides a comprehensive overview of Python programming basics, including code examples for printing, string manipulation, arithmetic operations, and various types of operators. It also covers functions, error handling, and demonstrates the use of loops and conditionals. Additionally, it explains different types of functions and their parameters, as well as common errors encountered in Python programming.

Uploaded by

pranshavji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Basic of Python

Ans-1)Print the code

Code:- print(“Hello”)

Ans-2)Single line

Code:-#print(“Hello”)

Print(“world”)

Ans-3)Double line

Code:- print("""hello,world,today""")
Ans-4)Static

Code:- a=10;

print("Value of a:",a)

Name Code:- name = “pranshav”

Print(“Your name :” , name)

5)Dynamic input from user(Phython)

Code:-name= input(“Enter your name:”)


Print(“Your name is:”,+name)

6)String demo(3 ways to declare string)

Code:- name1='Hello'

print('single quotes:'+name1)

name2="Hello,world"

print("single quotes:"+name2)

name3='''Hello,world,today'''

print('''single quotes:'''+name3)

#multi quotes

name4='''Hello,world,today'''
print('''single quotes:'''+name4)

* Operator (static and dynamic)

1)Arithmatic Operator:

-> a = 2

b=4

print('sum :', a + b)

print('Sub :', a - b)

print('Multi :', a * b)

print('Division :', a / b)

print('Floor division :', a // b)

print('Module :', a % b)

print('sum :', a ** b)
Input from user
a=int(input("Enter value a:"))
b=int(input("Enter value b:"))
print('sum :', a + b)
print('Sub :', a - b)
print('Multi :', a * b)
print('Division :', a / b)
print('Floor division :', a // b)
print('Module :', a % b)
print('sum :', a ** b)
Comparison Operator: (Static and dynamic)
Code:-
(Dynamic)
a=int(input("Enter value a:"))
b=int(input("Enter value b:"))
print('a == b = :', a == b)
print('a != b = :', a != b)
print('a <= b = :', a <= b)
print('a >= b = :', a >= b)
print('a < b = :', a < b)
print('a > b = :', a > b)

(Static Method)
a=5
b=6
print('a == b = :', a == b)
print('a != b = :', a != b)
print('a <= b = :', a <= b)
print('a >= b = :', a >= b)
print('a < b = :', a < b)
print('a > b = :', a > b)

3)Logical Operator (Static and dynamic method)


->Code:-
(Static)
a=5
b=6
print('Logical And:',a>5 and b<10)
print('Logical or:',a>5 or b<10)
print('Logical not:',not (a<10))

(Dynamic)
a=int(input("Enter value a:"))
b=int(input("Enter value b:"))

print('Logical And:',a>5 and b<10)


print('Logical or:',a>5 or b<10)
print('Logical not:',not (a<10))

2(Method)operational
Code:-
a=9
b=10
a += b
print('sum of a:',a)

sub:-
a=9
b=10
a -= b
print('sum of a:',a)

multi:-
a=9
b=10
a *= b

a=9
b=10
a /= b
print('sum of a:',a)

*)Identity operator
Code:-
Static Method
a=5
print(id(a))
b=5
print(id(b))
print(a is b)

a=5
print(id(a))
b=5
print(id(b))
print(a is not b)
* Membership operator:-
Code:-
a=1,2,3,4,5
print(6 not in a)
print(5 in a)
print(6 in a)

*)Syntax of string:

Code:-

a = "Panshav"

print(a)

a = 'Jobanputra'
print(a)

a = '''It is multiple string'''

print(a)

Output:-

*)Index of String :

Code:-

b="hello"

print(b[0])

print(b[1])

print(b[2])
print(b[3])

print(b[4])

Output:-

Output:-

*)String Operation:-

1)Repeatation String:-

Code:-

a = "Hello"

print(a*2)

Output:-
2)Concating of String:

Code:-

a = "Hello","World"

print(a+a)

Output:-

3)Slicing of String:-

Code:-

a = "Hello"

print(a[4])

Output:-
4)Range in slicing of string:-

Code:-

a = "Hello"

print(a[0:])

print(a[:3])

print(a[1:3])

Output:-

5)Membership of string:

Code:-

a = "Hello"
print("hello" in a)

print("Hello" not in a)

Output:-

*)String Method :

1)Upper Method:-

Code:-

a = "hello"

print(a.upper())

print(a)

Output:-
2)Lower Method:-

Code:-

a = "hello"

print(a.lower())

print(a)

Output:-

3)Is Upper Method :-

Code:-

a = "hello"

print(a.isupper())

print(a)
Output:-

4)Is lower Method:-

Code:-

a = "hello"

print(a.islower())

print(a)

Output:-

5)Split Method:-

Code:-

a = "1. hello"
print(a.split())

print(a)

Output:-

6)StartWith Method :-

Code:-

a = "hello"

b = a.startswith("hello")

b = a.startswith("H")

b = a.startswith("h")

print(b)
Output:-

7)End with Method:-

Code:-

a = "hello"

print(a.endswith("hello"))

print(a.endswith('e'))

Output:-

8)Join Method:-

Code:-

a = "hello,world"
b = ",".join(a)

print(b)

Output:-

9)Replace Method:-

Code:-

a = "hello"

b = a.replace("hello","Pranshav")

print(b)

Output:-

10)Find Method:-
Code:-

a = "hello, world"

print("Find Method:",a.find("world"))

print("Index Method:",a.index("world"))

Output:-

11)Count Method:-

Code:-

a = "hello,hello"

b = a.count("hello")

print(b)
b = a.count("e")

print(b)

Output:-

12)isAlpha:-

Code:-

a = "hello"

b = a.isalpha()

print(b)

b = a.isdigit()

print(b)
a = "12345"

b = a.isalpha()

print(b)

b = a.isdigit()

print(b)

Output:-

13)Capitalize Method:-

Code:-

a = "hello"

c = a.capitalize()
print(c)

b = "world"

d = b.capitalize()

print(d)

Output:-

*)While loop:-

Code:-

i=0

n = "Hello everyone"

while i < len(n):

if n[i] == 'e' or n[i] == 'y':


i = i+1

pass

print(i)

i = i+1

Output:-

*)Tuple create in while loop:-

Code:-

t1=(10,20,30,40,50)

i=0
while i < len(t1):

print(t1[i])

i = i+1

Output:-

*)Function start:-

Syntax Code:-

def demofunction():

print("Hello Welcome to function")

# Calling Function

demofunction()
Output:-

*)Type of Function:-

1)Default Function:-

Code:-

#1. Default Argument:

def default_function(a,b = 10):

print("Value a is:",a)

print("Value b is:",b)

#Calling the function and passing only one


argument

print("Passing only one argument.")


default_function(20)

#Now two arguments to the function

print("Passing two argument.")

default_function(40,50)

Output:-

2)Keyword Argument:-

Code:-

def keyword_function(a,b,c):

print("Value a is:",a)
print("Value b is:",b)

print("Value c is:",c)

#Without using keyword

print("Without using keyword.")

keyword_function(10,20,30)

#With using keyword

print("With using keyword.")

keyword_function(c=100,b=500,a=200)

Output:-
3)Required Argument:-

Code:-

def required_function(a,b,c):

print("Value a is:",a)

print("Value b is:",b)

print("Value c is:",c)

#Required function

required_function(1,'pranshav',18)

Output:-

4) Variablelength function:
Code:-

def variablelengthfunction(*args_list):

ans=[]

for i in args_list:

ans.append(i)

return ans

#passing args arguments

a=variablelengthfunction('Python','functio
n','programming')

print("variable length function call 1:",a)


b=variablelengthfunction(1,2,3,4,5)

print("variable length function call 2:",b)

c=variablelengthfunction('Python')

print("variable length function call 3:",c)

Output:-

5)Return Function:-

Code:-

def return_function(x):

return 5 * x #15

def return_fun(a,b):
ans = a+b

return ans

#Return type function print

print('Return type function


1:',return_function(2))

print('Return type function


2:',return_function(5))

Output:-

6)Multiple Return Function:-

Code:-
def multiple_return_fun():

a = 'today temp:',40

b = 'tomorrow temp:',35

c = 'tomorrow temp:',35

print(a,b,c)

return a,b,c

multiple_return_fun()

Output:-

7)Global Variable:-

Code:-

a=10
def demo1(a):

print("inside function a value:",a)

demo1(a)

print("Outside function a value:",a)

Output:-

8)Local Variable:-

Code:-

def test():

b=20

print("inside demo2:",b)
test()

Output:-

9)Dynamic Function:-

Code:-

def fetch_user_info():

#User id

user_id = int(input("Enter your ID:"))

#User Name

name = str(input("Enter your name:"))

#User age
age = int(input("Enter your age:"))

#print information

print("Name:",name)

print("ID:",user_id)

print("Age:",age)

#Call function

fetch_user_info()

Output:-

10)Built in function:-
...

len()

max()

min()

input()

range()

type()

print()

*)Error generated Type:-

1)Syntax error:-

Code:-
if (condition)

print("hello world")

Output:-

2)Name error:-

Code:-

if(n==3):

print("hello world")

Output:-
3)Indentation error:-

Code:-

if(n==3):

print("hello world")

Ouput:-

4)Type error:-

Code:-

a="pranshav" + 10

print(a)

Ouput:-
5) Value error:-

Code:-

a=int("hello")

print(a)

Output:-

6)Index error:-

Code:-

list=[1,2,3]

print(list[4])
Output:-

7)Key error:-

Code:-

a={"name":"Pranshav",}

print(a['no'])

Output:-

8)Attribute error:-

Code:-

a=[1,2,3]
a.append(4)

print(a)

a.push(5)

Output:-

9)import error:-

Code:-

import non_existent_module

Output:-

10)Division by zero error:-


Code:-

a=10 / 0

Output:-

*)Try and except :-

Code:-

def addnumbers(a,b):

try:

return(a+b)

except TypeError:

return("Invalid number")
print(addnumbers(1,2))

print(addnumbers(5,10))

print(addnumbers(7,'a'))

print(addnumbers(99,1))

print("Execution complete")

Output:-

*)Multiple error:-

Code:-

try:

a=int(input("Enter a number:"))
r=10/a

print("Result = ",r)

except ValueError:

print("Error: Invalid Input. Please Enter


a number.")

except ZeroDivisionError:

print("Error: Cannot division by 0.")

finally:

print("This block always executes")

Output:-

You might also like