Ternary operator
a=int(input())
b=int(input())
min=a if a < b else b
print(min)
#Arthematic operator:
x=int(input())
y=int(input())
print(x+y) #arthematic
print(x-y) #subtraction
print(x*y) #multiple
print(x/y) #divison
print(x%y) #module
Relational opearor:
x=int(input())
y=int(input())
print(x>y)
print(x>=y)
print(x<=y)
print(x==y)
print(x!=y)
Logical operator:
x=int(input())
y=int(input())
print(x and y)
print(x or y)
print(not y)
#Bitwise Operator:
x=int(input())
y=int(input())
print(x&y)
print(x|y)
print(x^y)
print(x<<y)
print(x>>y)
#Assignment operator
x=int(input())
y=int(input())
print(x+=y) #x=x+y
print(x-=y) #x=x-y
print(x*=y) #x=x*y
print(x/=y) #x=x/y
print(x%=y) #x=x%y
#Identity opertor:
x=int(input())
y=int(input())
print(x is y)
print(x is not y)
One Input From User:
a=input()
print(a)
Using split():
x,y,z =input().split()
print(x,y,z)
Using map function:
x=list(map(int,input().split()))
print(*x)
Another method :
x=[int(x) for x in input().split() ]
print(*x)
Combinations using methods:
from itertools import combinations
n=int(input())
c=combinations([1,2,3,4],n)
for i in list(c):
print(i)
# with an element-to-itself combination
from itertools import combinations_with_replacement
n=int(input())
c=combinations_with_replacement([1,2,3],n)
for i in list(c):
print(i)
#Permutations using methods:
* all permutations
from itertools import permutations
p=permutations([1,2,3,4])
for i in list(p):
print(i)
*permutations by using length.
from itertools import permutations
n=int(input())
p=permutations([1,2,3,4],n)
for i in list(p):
print(i)
#alternate letterby 1
def delo(arr):
for a in arr:
n=1
k=a[0]
for i in range(0,len(a),+2):
if(k!=a[i]):
n=0
break
return n
s=input()
print(delo(s))
#digit index:
s=input()
l=len(s)
for i in range(l):
if(s[i].isnumeric()):
print(i,end="")
#Another method:
def index(a):
res=""
for i in range(len(a)):
if(a[i]>'0' and a[i]<'9'):
res=res+str(i)
return res
s=input()
r=index(s)
print(r)
#FIND EMAIL:
s=input()
if '@' in s:
s=s.split('@')
print(s[1])
else:
print("Invalid Input")
#NUMBER OF EVEN NUMBERS IN ALPHANUMERIC:
s=input()
c=0
for i in range(len(s)):
if(s[i]=="0" or s[i]=="2" or s[i]=="4" or s[i]=="6" or s[i]=="8"):
c+=1
print(c)
#ANOTHER method
s=input()
c=0
for i in s:
if(i.isnumeric()):
if(int(i)%2==0):
c+=1
print(c)
BASICS OF PYTHON:
Eg1: To print Helloworld:
print("Hello World")
Eg2: To print the sum of 2 numbers
a=10
b=20
print("The Sum:",(a+b))
3.printing all keywords at once using "kwlist()"
import keyword
print("The list of keywords is : ")
print(keyword.kwlist)
4.Input From User
val = input("Enter your value: ")
print(val)
5.Input multiple values at a time.
a=list(map(int,input().split()))
print(a)
6.comments in python
single line: print("Hello, World!") #This is a comment
multi line: """
This is a comment
written in
than just one line
"""
print("Hello, World!")
7.variables
x = 5
y = "John"
print(x)
print(y)
8.casting:
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
9.Type of variable:
x = 5
y = "John"
print(type(x))
print(type(y))
10.IF_ELSE:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
11.FOR LOOP:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
a=[1,2,3,4,5]
for i in range(len(a)):
print(i)
12:WHILE LOOP:
i = 1
while i < 6:
print(i)
i += 1
13: Break Statement
-> the break statement we can stop the loop even if the while condition is true
i = 1
while i < 6:
if (i == 3):
break
print(i) #prints 1 and 2.
i += 1
14: Continue Statement
->the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i) #skip the value 3.
15. Functions
->No parameters in function:
def my_function():
print("Hello from a function")
my_function()
->passing parameters in function:
def my_function(food):
for x in food:
print(x) #prints apple,banana and cheery.
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
16.Lambda function
->Single variable
x = lambda a : a + 10
print(x(5)) #prints 15
->Multiple variables:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2)) #prints 13.
LISTS:
->Lists are used to store multiple items in a single variable.
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Opertions:
1.Create a list.
a=[1,2,3,4,5]
2.print a list
print(a) #print all elements
print(a[2]) #print only 3
print(a[2:4]) #print 3 and 4
print(a[2:]) #print 3,4,5.
print(a[:3]) #print 1,2,3
print(a[-4:-1]) #print 2,3,4
print(a[0:5:2]) #print 1,3,5[slice by 2]
print(a[-1]) #print 5
3.reverse of a list
a=[1,2,3,4,5]
print(a[::-1]) #[5,4,3,2,1]
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist) #print ['cherry', 'Kiwi', 'Orange', 'banana']
4.Length of a list
a=[1,2,3,4,5]
print(len(a)) #prints 5.
a=[1,2,3,4,5]
c=0
for i in a:
c+=1
print(c) # prints 5
5.Check item in list or not
a=[1,2,3,4,5]
if 3 in a:
print("yes")
6.Change list items
list = ["apple", "banana", "cherry"]
list[1] = "orange"
print(list) #prints apple orange cherry
list = ["apple", "banana", "cherry"]
list[1:3] = ["watermelon"]
print(list) #prints apple watermelon
7.Insert elements:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"] # prints apple banana watermelon and cherry
print(thislist)
8.Append:
l = [1,2,3,4,5]
l.append(20)
print(l) #prints [1,2,3,4,5,20] [append element at last]
9.extend:
l = [1,2,3,4,5]
l1=[10,20,30]
l.exends(l1)
print(l) #print [1,2,3,4,5,10,20,30]
10.Remove element
l = [1,2,3,4,5]
l.remove(2)
print(l) #print [1,3,4,5]
11.Pop
l = [1,2,3,4,5]
l.pop()
print(l) #print [1,2,3,4]
l = [1,2,3,4,5]
l.pop(3) #3 is index not elemnet
print(l) #print [1,2,3,5]
12.Clear
l = [1,2,3,4,5]
l.clear()
print(l) #prints []
13.loops in list
l=[1,2,3,4,5]
for i in l:
print(i,end=" ") #print 1 2 3 4 5
l=[1,2,3,4,5]
[print(i,end=" ") for i in l] #print 1 2 3 4 5
l=[1,2,3,4,5]
i=0
while(i<len(l)):
print(l[i])
i+=1 #print 1 2 3 4 5
14.List comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist) #print ['apple', 'banana', 'mango']
15.sort list
t= [100, 50, 65, 82, 23]
t.sort()
print(t) #print [23, 50, 65, 82, 100]
t= [100, 50, 65, 82, 23]
t.sort(reverse = True)
print(t) #print [100, 82, 65, 50, 23]
16.Copy list
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist) #print ["apple", "banana", "cherry"]
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist) #print ["apple", "banana", "cherry"]
17.List join
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Method | Description |
Converts the first character to upper case | |
Converts string into lower case | |
Returns a centered string | |
Returns the number of times a specified value occurs in a string | |
Returns an encoded version of the string | |
Returns true if the string ends with the specified value | |
Sets the tab size of the string | |
Searches the string for a specified value and returns the position of where it was found | |
Formats specified values in a string | |
format_map() | Formats specified values in a string |
Searches the string for a specified value and returns the position of where it was found | |
Returns True if all characters in the string are alphanumeric | |
Returns True if all characters in the string are in the alphabet | |
Returns True if all characters in the string are ascii characters | |
Returns True if all characters in the string are decimals | |
Returns True if all characters in the string are digits | |
Returns True if the string is an identifier | |
Returns True if all characters in the string are lower case | |
Returns True if all characters in the string are numeric | |
Returns True if all characters in the string are printable | |
Returns True if all characters in the string are whitespaces | |
Returns True if the string follows the rules of a title | |
Returns True if all characters in the string are upper case | |
Converts the elements of an iterable into a string | |
Returns a left justified version of the string | |
Converts a string into lower case | |
Returns a left trim version of the string | |
Returns a translation table to be used in translations | |
Returns a tuple where the string is parted into three parts | |
Returns a string where a specified value is replaced with a specified value | |
Searches the string for a specified value and returns the last position of where it was found | |
Searches the string for a specified value and returns the last position of where it was found | |
Returns a right justified version of the string | |
Returns a tuple where the string is parted into three parts | |
Splits the string at the specified separator, and returns a list | |
Returns a right trim version of the string | |
Splits the string at the specified separator, and returns a list | |
Splits the string at line breaks and returns a list | |
Returns true if the string starts with the specified value | |
Returns a trimmed version of the string | |
Swaps cases, lower case becomes upper case and vice versa | |
Converts the first character of each word to upper case | |
Returns a translated string | |
Converts a string into upper case | |
Fills the string with a specified number of 0 values at the beginning |