Monday 21 December 2020

Some famous Python one liner code

  Hi Guys !!! Hope all is well

I am going to list down some famous Python one liner code example.

1. Swap two variables
a = 1b = 2
ab = ba
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.
ab, *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 ff.write('hello world')
print(list(open('file.txt'))) #['sks1\n', 'sks2\n', 'sks3\n', 'sks4\n', 'hello world!']

7. List creation
# List creation 
list_1 = [('Hi 'yfor 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(6if 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(ifor i in [1,2,3]] )# [1.0, 2.0, 3.0]

13. Printing Patterns
n = 5
print('\n'.join('😀' * i for i in range(1n + 1)))

14. Prime Number
print(list(filter(lambda x:all(x % y != 0 for y in range(2x)), range(213))))

15. Find Max Number
findmax = lambda x,yx if x > y else y 
print(findmax(5,14))


Thanks
Saurabh 
Happy Coding !!!

3 comments:

  1. This post is so interactive and informative.keep updating more information...
    Java Course Duration
    Future Of Java Developer

    ReplyDelete
  2. 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

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...