Hello Everyone,
I got some cool stuff to share with you all. This tips will benefit those who just started exploring python. I kept things quite simple and straight forward for anyone to understand. Plus you can bookmark this blog page for your future reference in-case you forget something or get stuck.
So moving on with the code...
💥TOP 10 Python - Fun with Numbers💥
1) Creating variables in one line in python.
1 2 3 4 | >>> #_1 creating variables in one line in python >>> a = 5; b = 10; c = a; d = a**2 ; e = d + 10 >>> print(a,b,c,d,e) 5 10 5 25 35 |
2) Multiplying text with integer (number) in python.
1 2 3 4 | >>> #_2 multiplying text with integer (number) in python >>> a = 5 >>> print (a*'pysnake') pysnakepysnakepysnakepysnakepysnake |
3) Multiplying text with space in python.
1 2 3 4 | >>> #_3 multiplying text with space in python >>> a = 5 >>> print (a*'pysnake ') pysnake pysnake pysnake pysnake pysnake |
4) Multiplying text with line break '\n' in python.
1 2 3 4 5 6 7 8 | >>> #_4 multiplying text with line break '\n' in python >>> a = 5 >>> print (a*'pysnake\n') pysnake pysnake pysnake pysnake pysnake |
5) Print simple text with integer (tuple) in python.
1 2 3 4 | #_5 print simple text with integer (tuple) in python >>> a = 5 >>> print(a,'pysnake') 5 pysnake |
6) Python as a Calculator.
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> #_6 python as a Calculator >>> # Using Simple Operators:- [ + - * / ] >>> print(8+6, 20-7, 9*8, 7/2) 14 13 72 3.5 >>> >>> # Using floor functionality Operators:- [ % // ** ] >>> print(8%6, 20//7, 9**8) 2 2 43046721 >>> >>> # Using parentheses '( )' order of execution:- [ ( ) ] >>> print(25 - 3 / (7 % 2)) 22.0 >>> |
7) Using two main types of numbers in python i.e. 'int' (integer) & 'float' (numbers with decimal point).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #_7 using two main types of numbers in python i.e. 'int' (integer) & 'float' (numbers with decimal point) >>> planets = 12 >>> print(type(planets)) <class 'int'> >>> >>> pie = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067 >>> print(type(pie)) <class 'float'> >>> >>> # adding the two variables >>> planets_pie = planets + pie >>> print(planets_pie) 15.141592653589793 >>> >>> # convert to int >>> print(int(planets_pie)) 15 >>> >>> # convert to float >>> print(float(planets_pie)) 15.141592653589793 |
8) Using input function in python.
1 2 3 4 5 | >>> #_8 using input function in python >>> my_int = int(input("Enter a number:- ")) Enter a number:- 8 >>> print("Today's lucky number is..... :-",my_int) Today's lucky number is..... :- 8 |
9) Importing modules (Addons for Python). Reason:- For more functionality & faster calculation.
1 2 3 4 5 6 7 8 9 10 11 12 | >>> #_9 importing modules (Addons for Python). Reason:- For more functionality & faster calculation >>> # we will appoint import 'math' with 'm' & 'degrees' as 'd' as short hand word/words >>> import math as m >>> from math import degrees as d >>> print(m.sin(90)) 0.8939966636005579 >>> >>> print(m.cos(90)) -0.4480736161291701 >>> >>> print(m.tan(90)) -1.995200412208242 |
9.1) Printing Pie
1 2 3 | >>> # printing pie >>> print(m.pi) 3.141592653589793 |
9.2) Convert to degree
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> # convert to degree >>> print(d(m.pi/2)) 90.0 >>> # convert to degree + int >>> print(int(d(m.pi/2))) 90 >>> # convert to degree + int + string >>> print(str(int(d(m.pi/2)))) 90 >>> # convert to degree + int + string + '°' >>> print(str(int(d(m.pi/2)))+'°'+' degree') 90° degree >>> |
9.3) Printing factorial
1 2 3 4 5 6 7 | >>> # printing factorial >>> print(m.factorial(2)) 2 >>> print(m.factorial(3)) 6 >>> print(m.factorial(7)) 5040 |
10) Using number range in python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | >>> #_10 using number range in python >>> # print factorial ranging from 1 to 50 using (start, stop, step) method >>> start = 1 >>> stop = 50 >>> step = 1 >>> for i in range(start,stop+1,step): # Reason:- it will stop till 49 if +1 is not added! print(str(i)+' != '+str(m.factorial(i)),end='\n') 1 != 1 2 != 2 3 != 6 4 != 24 5 != 120 6 != 720 7 != 5040 8 != 40320 9 != 362880 10 != 3628800 11 != 39916800 12 != 479001600 13 != 6227020800 14 != 87178291200 15 != 1307674368000 16 != 20922789888000 17 != 355687428096000 18 != 6402373705728000 19 != 121645100408832000 20 != 2432902008176640000 21 != 51090942171709440000 22 != 1124000727777607680000 23 != 25852016738884976640000 24 != 620448401733239439360000 25 != 15511210043330985984000000 26 != 403291461126605635584000000 27 != 10888869450418352160768000000 28 != 304888344611713860501504000000 29 != 8841761993739701954543616000000 30 != 265252859812191058636308480000000 31 != 8222838654177922817725562880000000 32 != 263130836933693530167218012160000000 33 != 8683317618811886495518194401280000000 34 != 295232799039604140847618609643520000000 35 != 10333147966386144929666651337523200000000 36 != 371993326789901217467999448150835200000000 37 != 13763753091226345046315979581580902400000000 38 != 523022617466601111760007224100074291200000000 39 != 20397882081197443358640281739902897356800000000 40 != 815915283247897734345611269596115894272000000000 41 != 33452526613163807108170062053440751665152000000000 42 != 1405006117752879898543142606244511569936384000000000 43 != 60415263063373835637355132068513997507264512000000000 44 != 2658271574788448768043625811014615890319638528000000000 45 != 119622220865480194561963161495657715064383733760000000000 46 != 5502622159812088949850305428800254892961651752960000000000 47 != 258623241511168180642964355153611979969197632389120000000000 48 != 12413915592536072670862289047373375038521486354677760000000000 49 != 608281864034267560872252163321295376887552831379210240000000000 50 != 30414093201713378043612608166064768844377641568960512000000000000 >>> |
10.1) Using Python Exp
1 2 3 4 5 6 7 8 | >>> # using python exp (calculate power of E-Euler's number approx equal to 2.71828) >>> m.exp(1) 2.718281828459045 >>> m.exp(2) 7.38905609893065 >>> m.exp(3) 20.085536923187668 >>> |
10.2) Using Python Log
1 2 3 4 5 6 7 8 | >>> # using python log (returns natural logarithm of x, for x > 0) >>> m.log(16,2) 4.0 >>> m.log(80,20) 1.4627564263195183 >>> m.log(462,7) 3.1530566270289455 >>> |
10.3) Using Python Floor
1 2 3 4 5 6 7 8 | >>> # using python floor (return the closest integer value which is less than or equal to the specified expression or value) >>> m.floor(1) 1 >>> m.floor(4.6) 4 >>> m.floor(3.3216484) 3 >>> |
10.4) Using Python SQRT (Square Root)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | >>> # using python sqrt (return square root of x) >>> m.sqrt(1) 1.0 >>> m.sqrt(2) 1.4142135623730951 >>> m.sqrt(3) 1.7320508075688772 >>> m.sqrt(4) 2.0 >>> m.sqrt(5) 2.23606797749979 >>> m.sqrt(6) 2.449489742783178 >>> m.sqrt(7) 2.6457513110645907 >>> m.sqrt(8) 2.8284271247461903 >>> m.sqrt(9) 3.0 >>> m.sqrt(10) 3.1622776601683795 >>> |
Hope you like the tutorial.
Python is simple and fun to play with and yet very powerful. 🔥