Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Python Numbers
Numbers are more often used in programming to do calculations or store data. For example, if you are developing a Python game, you need to keep the scores calculated. Python integers are the simplest form of numbers that you can use. I will do some calculations by using different mathematical operators. a = 3 + 6 print(a) a = 10 - 5 print(a) a = 3 * 6 print(a) a = 18 / 6 print(a) a = 3 ** 6 print(a) a = 15 ** 6 print(a) = RESTART: C:/Users/saifia computers/Desktop/sample.py 9 5 18 3.0 729 11390625 >>> In the next example, I will use multiple operators to see how Python maintains the order of mathematics. a = 3 + 6 * 18 print(a) a = 10 - 5 + 45 print(a) a = 3 * 6 + 50 / 30 print(a) a = 18 / 6 * 50 print(a) a = (3 * 6 )- 23 print(a) a = (15 ** 6) + 100 print(a) = RESTART: C:/Users/saifia computers/Desktop/sample.py 111 50 19.666666666666668 150.0 -5 11390725 >>> The next number type is float. This is the decimal form of integer. See how to use them. a = 3.0 + 6.344 * 18.234 print(a) a = 10.1 - 5.56 + 45.22 print(a) a = 3.45 * 6.3 + 50.09 / 30.1 print(a) a = 18.22 / 6.1 * 50.89 print(a) a = (3.21 * 6.23 )- 23.22 print(a) a = (15.123 ** 6.23) + 100.456 print(a) = RESTART: C:/Users/saifia computers/Desktop/sample.py 118.67649600000001 49.76 23.399119601328902 152.00259016393443 -3.2216999999999985 22343252.55463075 >>> When you mix up the datatypes without proper procedure, you get an error in return. For example, you have to mix up date with day to display a string statement. This will trigger an error if you do not fill in the code with the right datatype and through the right process. place_name = "Silicon Valley" state_name = "California" country_name = "United States" location_info = place_name + " , " + state_name + " , " + country_name date = 4 info = "Hi, I want to visit " + location_info.title() + " in the next month on" + date + " th." print(info) = RESTART: C:/Users/saifia computers/Desktop/sample.py Traceback (most recent call last): File "C:/Users/saifia computers/Desktop/sample.py", line 6, in info = "Hi, I want to visit " + location_info.title() + " in the next month on" + date + " th." TypeError: can only concatenate str (not "int") to str >>> The error is a type error. Python is unable to recognize the information you have put in. In simple words, the wrong method to fill in the code has confused Python. So, you have to turn the integer into a string first to display it in the right way. The technique is simple, but you will have to memorize it. place_name = "Silicon Valley" state_name = "California" country_name = "United States" location_info = place_name + " , " + state_name + " , " + country_name date = 4 info = "Hi, I want to visit " + location_info.title() + " on the " + str(date) + "the of July" print(info) = RESTART: C:/Users/saifia computers/Desktop/sample.py Hi, I want to visit Silicon Valley, California, United States on the 4th of July >>> I have told python that I have to add an integer to the string statement. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling