Hi Guys !!! Hope all is well
I am going to list down some famous Python one liner code example.
1. Swap two variablesa = 1; b = 2
a, b = b, a
print(a,b) #>> 2 1
2. Multiple variable assignment
Here you can use it to assign list elements to the given variables, which is also called unpacking. The * will do packing the remaining values again, which results in a sublist for c.
a, b, *c = [1,2,3,4,5]
print(a,b,c) #>> 1 2 [3, 4, 5]
3. Sum over every second element of a list
a = [1,2,3,4,5,6]
s = sum(a[1::2])
print(s) #>> 12
4. Delete multiple elements
slicing syntax can also be used to delete multiple list elements at once
a = [1,2,3,4,5]
del a[::2]
print(a) #>> [2, 4]
5. Read file into array of lines
c = [line.strip() for line in open('file.txt')]
print(c) #>> ['sks1', 'sks2', 'sks3', 'sks4']
6. Write string to file
with open('file.txt', 'a') as f: f.write('hello world')
print(list(open('file.txt'))) #['sks1\n', 'sks2\n', 'sks3\n', 'sks4\n', 'hello world!']
7. List creation
# List creationlist_1 = [('Hi '+ y) for y in ['sks', 'mks','pks']]print(list_1) #>> ['Hi sks', 'Hi mks', 'Hi pks']
8. List mapping
l = list(map(int, ['1', '2', '3']))print(l) #>> [1, 2, 3]
You can also use Pythons map() function to cast every list element to another type.
9. Set creation
squares = { x**2 for x in range(6) if x < 4 }print(squares) #>> {0, 1, 4, 9}
10. Palindrome check
phrase = 'deleveled'isPalindrome = phrase == phrase[::-1]print(isPalindrome) #>> true
11. Sum of Even Numbers In a List
a = [1,2,3,4,5,6]
s = sum([num for num in a if num%2 == 0])
print(s)
12. Creating Lists
lst = [i for i in range(0,10)]
print(lst) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# or
lst = list(range(0,10))
print(lst)
13. Mapping Lists or TypeCasting Whole List
print(list(map(int,['1','2','3']))) #[1, 2, 3]
print(list(map(float,[1,2,3]))) # [1.0, 2.0, 3.0]
print([float(i) for i in [1,2,3]] )# [1.0, 2.0, 3.0]
13. Printing Patterns
n = 5
print('\n'.join('😀' * i for i in range(1, n + 1)))
14. Prime Number
print(list(filter(lambda x:all(x % y != 0 for y in range(2, x)), range(2, 13))))
15. Find Max Number
findmax = lambda x,y: x if x > y else y
print(findmax(5,14))
Thanks
Saurabh
Happy Coding !!!
This post is so usefull and informative.keep updating with more information...
ReplyDeleteSwift Developer Course in Mumbai
Swift Developer Course in Ahmedabad
Swift Developer Course in Cochin
Swift Developer Course in Trivandrum
Swift Developer Course in Kolkata
This post is so interactive and informative.keep updating more information...
ReplyDeleteJava Course Duration
Future Of Java Developer
Every source code written in Java is written first using plain text documents within the Java programming language, ending with a .java extension. It is possible to write it using any text editor or notepad. These source files are transformed into .class files using Javac's compiler. A .class file doesn't contain the native code of your processor. Instead, it contains bytecodes that are part of the machine language used by the Java Virtual Machine1 (Java VM). The Java Launcher tool executes your program using an actual instance of it. Java Virtual Machine. So, the java platform is dependent.
ReplyDelete