2. Begin programming in python

Display the message in Python

print("Hello Welcome to first python Lab")

Variable:

Variables are the temporary location which is used to store a values

name="Ram" # here name is a string type variable

print(name)

type(name) #type of variable is decided by run time, which means what kind of values are being assinged.

O/p

Ram

str

name=10

type(name)

O/p

int

Keywords : Predefined words which are used to write the code

import keyword # imorting keyword library( package)

print(keyword.kwlist)

O/p

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Program to perform addition of two number

x=10

y=20

sum=x+y

print(sum)

O/p

30

print("Addition of two number", sum)

O/P

Addition of two number 30

Multiplication

x=2*3*4

print(x)

O/P

24

print(1,2,3,4)

o/p

1 2 3 4

print(1,2,3,4, sep='*')

o/p

1*2*3*4

Performing the addition of two number by accepting the input from user

n1= int(input("Enter the value for n1="))

n2=int(input("Ënter the value for n2="))

sum=n1+n2

print("Addition of n1 and n2=", sum)

output

Enter the value for n1=10

Ënter the value for n2=20

Addition of n1 and n2= 30

Different operation related to operator

Different kind of operators:

1) Arithmetic operators ( +, -, /,*)

2) logical operators

3) Bitwise operators

n1= int(input("Enter the value for n1="))

n2=int(input("Enter the value for n2="))

Output

Enter the value for n1=30

Enter the value for n2=12

print("Multiplication", (n1*n2))

Output

Multiplication 360

print("Modulus", (n1%n2))

Output

Modulus 6

print("Exponent",(n1**n2))

output

Exponent 531441000000000000

print("Division",(n1/n2))

Output

Division 2.5

print("Floor Division",(n1//n2))

Output

Floor Division 2

Comparison( Relational) Operators

1) ==

2) !=

3) >

4) <

5) >= or <=

x=10

y=11

print("x==y is ", (x==y))

print("x!=y is ", (x!=y))

Output

x==y is False

x!=y is True

Bitwise Operators

1) & (bitwise and)

2) | (bitwise or)

3) ^ (bitwise XOR)

4) ~ ( binary one's complement)

5) << ( left shift)

6) >> (right shift)

a,b=6,2 # six binary =110

# Two binary =010

print(a,b)

print("Bitwise AND",(a&b))

print("Bitwise OR",(a|b))

Output

6 2

Bitwise AND 2

Bitwise OR 6

Logical Operators

1) and (&&)

2) or (||)

3) not (~)

a=10

b=5

c=3

d=1

# a,b,c,d=10,5,3,1

print((a>b)and(c>d))

print((a>b)or(d>c))

Output

True

True

Data Types

Data stored in a memory location may he many types.

1) numbers

2) String

3) list

4) Tuple

5) set

6) Dictionary


1) Numbers

b=1.6

type(b)

output

float

d=x+9j

type(d)

Output

complex

Mathematical Functions


import math

n=-2.3456

print(abs(n))

Output

2.3456

x=12

print(math.sqrt(x))

Output

3.4641016151377544

print(math.ceil(12.3))

output

13

print(math.floor(12.3))

Output

12

print(math.pow(2,5))

Output

32

print(math.exp(3))

Output

20.085536923187668

print(math.log(12.3))

Output

2.509599262378372

print(min(12,3,4,5))

Output

3

print(max(12,3,4,5))

Output

12

print(round(12.64545))

Output

13