Python Tips and Tricks: Every Python Programmer Must Know




[ad_1]

Here are some cool tips and tricks which is used while making a project or program.

10. Adding 2 list :
for this you just have to create 2 list and then combine them in 2 variables. i.e. a=[1,2,3] b=[4,5,6] then c=a+b. Now c can hold 2 list.

9: Calendar:
In this trick you can print calendar according to calendar format like import calendar then print calendar.month(2017,1) you can also pass variable to it according to your format.

8: Swap:
In other languages you have to write multiple lines of code for it but in Python you can just do it by single line i.e a,b=b,a that’s the beauty of python.

7: List:
In python you can convert any string into list by using list function i.e a=’abcd’ then a=list(a). If your list contains integer values then just convert it by using str() function i.e a=123 then a=str(a) now you can convert it into list.

6: Join:
You can join list elements by using join function i.e. ”.join(msg) also if you pass any value within ” then it join with all elements.

5: Date Time:
This is the essential thing for programmer to show it any project or program just import datetime module and call the function and print it (by watching the video) also you can change the format of it %d for date, %m for month, %y for year, %I for hour, %M for minute, %s for second, %p for 12 hour.

4. Random :
using random function is essential in making games, cryptography,projects etc , in python import random module and select your random value via random.choice(variable). You can also select list or any type of value.

3: Color :
A program can become more attractive if it has a beautiful color in it. Just import os module and put os.system(‘color A’) and set color according to you.
Color code in os are:
0: black
1: blue
2: green
3: aqua
4: red
5: purple
6: yellow
7:white
8: gray
9: light blue
A: light green
B: light aqua
C: light red
D: light purple
E: light yellow
F: Bright white

2: Compress:
Sometimes you have to compress your messages or values in your program, by python you can do it eaisly, Just import zlib module and pass your message in zlib.compress(msg), it compress your msg, to decompress just use zlib.decompress(msg) to decompress it.

1: Message Box:
To make a program user friendly, this is very useful, just import ctypes and write ctypes.windll.user32.MessageBoxA(0,’Your Message’,’Message title’,0) here 0 is for ok button. You can also use tkinter but i like this though.

Support me to develop more new free cool stuff: https://www.paypal.me/sandeeppushp

So guys that’s all in this video. Hope you like it .
Thumbs up if you like it and subscribe to get more new stuff.

Source


[ad_2]

Comment List

  • Sandeep Pushp
    November 17, 2020

    These are a great set of tips!

  • Sandeep Pushp
    November 17, 2020

    Thanl you man OMG how didnt I learn those before i have been learning python for the past month

  • Sandeep Pushp
    November 17, 2020

    most of them doesnt work in python 3 btw

  • Sandeep Pushp
    November 17, 2020

    Awesome

  • Sandeep Pushp
    November 17, 2020

    Nice tips. was wondering what the last track of music was, I like it.

  • Sandeep Pushp
    November 17, 2020

    Nice tips for PYTHON . Can you make a tips video for C language and LINUX commands ?

  • Sandeep Pushp
    November 17, 2020

    Thank

  • Sandeep Pushp
    November 17, 2020

    Nice tips, just a note:
    Pytho 3.7+ zlib don't compress string:
    TypeError: a bytes-like object is required, not 'str'

    You need encode to byte and then compress:
    import zlib

    foo = 'Lorem ipsum dolor sit amet'
    foo_byte = foo.encode() # default encode uft-8

    # compress byte
    compressed = zlib.compress(foo_byte)
    print(compressed)
    # b'xx9cxf3xc9/JxcdUxc8,(.xcdUHxc9xcfxc9/R(xce,QHxccM-x01x00x83xd5txc5'

    # decompress byte
    decompressed = zlib.decompress(compressed)

    print(decompressed)
    # b'Lorem ipsum dolor sit amet'

    print(decompressed.decode()) # default decode uft-8
    # Lorem ipsum dolor sit amet

  • Sandeep Pushp
    November 17, 2020

    great

  • Sandeep Pushp
    November 17, 2020

    Thank You

  • Sandeep Pushp
    November 17, 2020

    this is good

Write a comment