Python Stuff -1
Hello Everyone, Hope you are doing well. Actually, in this pandemic, I thought to learn about python programming. I am a new python learner so I think what if I write a blog as a note. If I made any mistakes please let me know. I will correct that.
· in — if left operand is in the right operand or not? If found then true otherwise false
a = ‘Bangladesh’
b=’desh’
b in a
the output will be true because the value of ‘b’ is found in ‘a’
a in b
the output will be false because the value of ‘a’ is not found in ‘b’
· not in — if it does not finds a variable in the specified sequence then it is true otherwise false
a = 5
list = [1,4,5,10,12,15 ];
if ( a not in list ):
print(“a is NOT present in given list”)
else:
print(“a is present in given list”)
String
a = ‘bangla’
a[1:4]
output is = ‘ang’
here 1:4 means it will access only the index of 1,2,3 not 4. That means it will start from 1 and stop at less than 4
· capitalize () — It makes the first character of a string Upper Case. (Only first character)
a = ‘akash’
a.capitalize()
output = Akash
So we can see that A is upper case character
· upper() — it makes all the charatcer upper case letter
a = ‘akash’
a.upper()
output = AKASH
· lower() & casefold() — it makes all the charatcer lower case letter
a = ‘AkAsh’
a.lower()/a.casefold()
output = akash
· title()- it makes the first character of all the word upper case
txt = “Welcome to my world”
x = txt.title()
print(x)
output will be = Welcome To My World
· swapcase()- it makes upper character to lower character and lower character to upper character
a = ‘BangLA’
a.swapcase()
output = bANGla
· len() — It find length of any string
a = ‘akash’
a.len()
output = 5
· count()-it returns the number of elements with the specified value.
number = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = number.count(1)
print(x)
output = 2
· find() / index() — it returns the number of index of a specified character or value.
str = “Hello, Welcome to my world.”
x = str.find(“o”)/ str.index()print(x)
output = 4
· replace() -it replaces a specified phrase with another specified phrase
sen = ‘How are you?’
sen.replace(‘o’,’d’)
Get Saidur Rahman Akash’s stories in your inbox
Join Medium for free to get updates from this writer.
Output = Hdw are ydu?
· strip()- If the parameters in the function match the first and last characters of the string, the new string returns to the previous string by omitting them.[ removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)]
str = “ banana “
x = str.strip()
print (“of all fruits”, x, “is my favorite”)
Output = of all fruits banana is my favorite
· extend() — add the specified list elements to the end of the current list.
fruits = [‘apple’, ‘banana’, ‘cherry’]
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
fruits.extend(cars)
print(fruits)
output = [‘apple’, ‘banana’, ‘cherry’, ‘Ford’, ‘BMW’, ‘Volvo’]
· del() — it is used to delete objects/elements
x = [“apple”, “banana”, “cherry”]
del x[0]
print(x)
output = [‘banana’, ‘cherry’]
· remove() — it’s removes the first occurrence of the element with the specified value.
fruits = [‘apple’, ‘banana’,’banana’, ‘cherry’]
fruits.remove(“banana”)
print(fruits)
output = [‘apple’, ‘banana’, ‘cherry’]
· discard() -The discard() method removes the specified item from the set.
This method is different from the remove () method, because the remove() method will raise an error if the specified item does not exist, and the discard() method will not.
A = {‘orange’, ‘banana’, ‘pear’}
A.discard(‘berry’)
Output = {‘orange’, ‘banana’, ‘pear’}
· pop() — it gives first element of the set.
A = {‘pear’, ‘banana’, ‘apple’, ‘orange’}
A.pop()
Output = ‘pear’
· clear() — it removes all the elements from set.
A = {‘r’, ‘apple’, ‘pineapple’, ‘b’, ‘orange’, ‘banana’, ‘y’, ‘g’, ‘a’, ‘p’, ‘berry’, ‘grape’, ‘e’, ‘pear’}
A.clear()
set()
· union() — The union() method returns a set that contains all items from the original set, and all items from the specified sets. If an item is present in more than one set, the result will contain only one appearance of this item.
x = {“a”, “b”, “c”}
y = {“f”, “d”, “a”}
z = {“c”, “d”, “e”}
result = x.union(y, z)
print(result)
Output = {‘f’, ‘a’, ‘b’, ‘c’,’d’, ‘e’}
· intersection () — The intersection() method returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.
x = {“a”, “b”, “c”}
y = {“c”, “d”, “e”}
z = {“f”, “g”, “c”}
result = x.intersection(y, z)
print(result)
Output = {‘c’}
· difference () — The difference() method returns set contains items that exist only in the first set, and not in both sets.
x = {“apple”, “banana”, “cherry”}
y = {“google”, “microsoft”, “apple”}
z = x.difference(y)
print(z)
Output = {‘banana’, ‘cherry’}
· pass — When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed
a = 33
b = 200
if b > a:
pass







