PYTHON
PYTHON
PYTHON
History:
Data types
Type casting
var1="34.25"
var2="54.42"
#print(var1+var2)
print(float(var1)+float(var2))
print("\n",10*str(float(var1)+float(var2)),"\n")
Operators:
Logical Operator
Bitwise operator
# Assignment
d=7
d+=3
print("+=",d)
d-=4
print("-=",d)
d*=5
print("*=",d)
d/=8
print("/=",d)
d%=12
print("%=",d)
d**=4
print("**=",d) # expotional digit 7 power of 4
# Arithmatic
d=2
print("==",d)
a=3
b=5
print("+",a+b)
print("-",a-b)
print("/",a/b)
print("//",a//b)
print("**",a**b)
print("%",a%b)
print("*",a*b)
# Bitwise
p=1
q=2
print("&",p&q) #
print(~4)
print(3<<2)
print(4>>3)
print(1|2)
print(8&3)
print("^",2^3)
print("<<",q<<p)
print(">>",p>>q)
# Logical
x=2
y=3
z=1
print(x>y and y>z)
print(y>x or x>y)
# Identity
a=[10,20,30]
b=[10,20,30]
print(a is not b)
d=[40,50,60]
print(a is not d)
print (a is d)
# Membership
# in , not in
list1 = ["hii","hello"]
print("hii" in list1)
print("jiii" not in list1)
Built in Function:
Python comes with a wide range of built-in functions that
are readily available for use. Here are some commonly
used built-in functions in Python:
1. **`print()`**: Outputs the specified message to the console.
```python
print("Hello, Python!")
```
```python
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
```
```python
x = 10
data_type = type(x)
```
```python
x = int("123")
y = float("3.14")
z = str(42)
```
```python
name = input("Enter your name: ")
```
```python
numbers = range(1, 6) # Generates 1, 2, 3, 4, 5
```
```python
my_list = [1, 2, 3, 4, 5]
total = sum(my_list)
```
8. **`max()`**, **`min()`**: Returns the maximum or minimum
value in an iterable.
```python
numbers = [4, 1, 7, 3, 9]
max_value = max(numbers)
min_value = min(numbers)
```
```python
x = -10
absolute_value = abs(x)
```
**`sorted
10. ()`**: Returns a sorted list from the specified iterable.
```python
unsorted_list = [3, 1, 4, 1, 5, 9, 2]
sorted_list = sorted(unsorted_list)
```
List
Tuple
a = (1, 2, 5, "Rise")
b = (5, "CSE")
print(a)
print(a[0])
print(a[0:2])
print(a[1:])
print(b*2)
print(a + b)
print(max(d))
print(min(d))
print(sum(d))
index = d.index(20)
print(index)
Dictonary:
- **Unordered Collection:** Dictionaries are an unordered
collection of items.
dic={'name':"ABC",'code':17,'dept':"CSE"}
print(dic['name'])
print(dic)
print(dic.keys())
print(dic.values())
new=dic.copy()
print(new)
seq=('code','dept','name')
print(dic.fromkeys(seq))
print(len(dic))
print(dic['name'])
dic['name']="DXA"
print(dic)
dic['key']='value'
print(dic)
print(dic.popitem())
print(dic)
del dic['code']
print(dic)
dic2={1:'one',2:'two'}
print(dic2)
Global Keyword:
In Python, the global keyword allows you to change a
variable value outside of its current scope. It is used to
make changes to a global variable from a local location.
The global keyword is only required for altering the
variable value and not for publishing or accessing it.
d=89
def fun1():
d=20
def fun2():
global d
d=88
print("After calling fun2()", d)
print("before calling fun2()",d)
fun2()
fun1()
print(d)
Map() :-
A map() function executes certain instructions- or
functionallity provided to it on every item of an
iterable.
The iterable could be a list, tuple, set, etc. It is worth
nothing that the output in the case of the map is abo an
iterable ie, a list it is a built-in function, so no import
statement required.
a=[1,2,3,4,5]
b=list(map((lambda x:x**3),a))
print(b)
Filter():-
"A filterci in python tests a specific wer- defined condition
for function or returns an iteable for the elements and
values that satisfy the condition or, in other words, return
true.
c=[7,8,9,1,2,3,4,5]
d=list((filter(lambda x:x in a,c)))
print(d)
Reduce():
import functools
q=["2", "4","6","8","10"]
p=functools.reduce((lambda x,y:x+y),q)
print(p)
Adavance Slicing :
mystr="Welcometopythonprogramming"
print(len(mystr))
print(mystr[0:6])
print(mystr[11:17])
print(mystr[0:29:2])
print(mystr[0:70])
print(mystr[: :-1])
var4="pas1s"
print(var4.isalnum())
print(mystr.isalpha())
Recursion :
def fac_iterative(n):
fact=1
for i in range (n):
fact=fact*(i+1)
return fact
def factorial_recussive(n):
if n==1:
return 1
else:
return n*factorial_recussive(n-1)
number=int(input("Enter no"))
print("factorial iterative method",fac_iterative(number))
print("factorial recussive",factorial_recussive(number))
Short If
a=int(input((" Enter a number")))
b=int(input((" enter a number")))
print("A is greater") if(a>b) else print(" B is greater")
String Funcions
var="Good Morning"
print(var.upper())
print(var.lower())
mystr="Welcome to python programming"
print(mystr.replace("python","java"))
print(mystr.count("g"))
print(mystr.find("pro"))
print(mystr.endswith("d"))
print(mystr.split())
a=10
b=20
a,b=b,a
print(a,b)
print("a=",a,"b=",b)
a,b=b,a
print("Ater a=",a,"b=",b)
python
# Writing to a file using 'with' block
with open('example.txt', 'w') as file:
file.write('Hello, this is a sample text.')
with open("f1.txt","r") as f:
print(f.readlines())