fbpx
Tricks

Python Tricks Every Beginners Should Know

#1 Reverse a string

in this method we are going to learn how to reverse a string, in just 2 line of code, i think this is very cool method to reverse a strings in Python.

#2 Swap two numbers with one line

Swap two number in just in two line, in this method we use two variables to initialize two numbers, just like we have “x” variable which have the value 44 and “y” variable which have the value 66 now we swap the number without temp variable.

Now just initialize “x” value in “y” and “y” value in “x” and print the value of (x.y).

#2 Check the memory usage of an object

In this method, we learn how to get the size of any type of data types size. This method very uses full to manage your memory usages in your program.

In this method, we import “sys”. Which is help users to get the size of the objects? we use the syntax “print(sys.getsizeof(num) )” and we get the how much memory is usages by the object.

#3 Print string N times

In this method, we use two-variable “s”, “n” we going to initialize any string in s. And any numbers in n, and then we use print() function and print the s*n which gives are N numbers of strings.

#4 Create a single string from all the elements in list

In this method we join the all elements of the list and print them.

#5 Store all three values of the list in 3 new variables

in this method we make a list of a which is hold 1,2, and 3 number value and we initialize this list value in three variable which is x,y, and z. And then we print the x,y, and z.

Share This Post To Your Friends

Similar Posts

Leave a Reply

Your email address will not be published.