Typecasting is required to convert a value from one data type to another. You just saw an example in the last section where the value of price and quantity obtained by the user were in string format and were converted into integer and float values respectively with the help of typecasting.Typecasting is a very simple and straightforward approach used to convert a variable from one data type to another...
Friday, June 30, 2023
Monday, June 26, 2023
Possibilities of printing output
Notice the difference in print command in the case (I) and (II). Both print the same message.1. The string message and variable values are separated by commas.qty = int(input("How many apples do you have? : "))price = float(input("What is the total cost? : "))value_of_one = price/qtyprint(qty,' apples cost ',price,' therefore one apple costs',value_of_one,'.')Output:How many apples do you have? :...
Friday, June 23, 2023
Getting the user input and displaying output
Python has an in-built input() function that has an optional argument which is a prompt string. A prompt string can be a message for the user on the prompt that tells him/her about the value that he/she must provide.During the execution of a program, if the interpreter encounters the input() method, it halts the program till the user provides input and hits the enter key.Syntax>>>input([prompt])Look...
Monday, June 12, 2023
Date/Time module
Following is the code to retrieve current date and timeimport datetimeprint(datetime.datetime.today())Orimport datetimeprint(datetime.datetime.now())Code to display current hour of the dayfrom datetime import *print(getattr(datetime.today(),'hour'))Code to print current local dateIn order to display the present local date we can use of today() method which is defined in the date class.import datetimedate_object...
Friday, June 9, 2023
The random numbers
To work with random numbers you will have to import random module.The following functions can be used:import random#Random choiceprint (" Working with Random Choice")seq=[8,3,5,2,1,90,45,23,12,54]print ("select randomly from ", seq," : ",random.choice(seq))print("*******************")#randomly select from a rangeprint ("randomly generate a number between 1 and 10 :",random.randrange(1, 10))print("*******************")#random()print...
Monday, June 5, 2023
The math module - trigonometric functions
Some of the trigonometric functions defined in the math module are as follows:import math# calculate arc tangent in radiansprint ("atan(0) : ",math.atan(0))print("**************")# cosine of xprint ("cos(90) : ",math.cos(0))print("**************")# calculate hypotenuseprint ("hypot(3,6) : ",math.hypot(3,6))print("**************")# calculates sine of xprint ("sin(0) : ", math.sin(0))print("**************")#...
Friday, June 2, 2023
The math module - Mathematical functions
Mathematical functions are defined in the math module. You will have to import the math module to get access to all its functions.import math#ceiling valuea = -52.3print ("math ceil for ",a, " is : ", math.ceil(a))print("********************")#exponential valuea = 2print("exponential value for ", a, " is: ",math.exp(2))print("********************")#absolute value of xa = -98.4print ("absolute value...