Friday, June 30, 2023

Typecasting

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 data type.

Let’s say there are three variables num1, num2, and num3 as follows:

>>> num1 = 4

>>> type(num1)

<class 'int'>

>>> num2 = 3.6

>>> type(num2)

<class 'float'>

>>> num3 = 'dog'

>>> type(num3)

Now, let’s try to change the value of each variable to another type.

>>> num11 = float(4)

>>> num11

4.0

>>> type(num11)

<class 'float'>

>>> num22 = str(num2)

>>> type(num22)

<class 'str'>

>>> num33 = int(num3)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

num33 = int(num3)

ValueError: invalid literal for int() with base 10: 'dog'

>>>

So, you see, you can convert numeric from one type to another and you can convert a numeric value to a string value but a string value cannot always be converted to a numeric value. Only if the string consists of a numeric value, can it be converted to a numeric data type. Following are some examples of typecasting a string into numeric values. Do have a look and don’t forget to experiment yourself.

a.

>>> str1 ='3.7'

>>> float(str1)

3.7

b.

>>> str2 = '3.7'

>>> int(str2)

Traceback (most recent call last):

File "<pyshell#16>", line 1, in <module>

int(str2)

ValueError: invalid literal for int() with base 10: '3.7'

c.

>>> str4 = '4.8'

>>> x = float(str4)

>>> int(x)

4

>>>

The problem in the example (b) can be resolved by first converting the string to float and then converting the float value to integer. This is shown in example(c) and another way is as follows:

>>> str2 = '3.7'

>>> int(float(str2))

3

>>>

Now that you have learned about typecasting, you would have also understood the importance of typecasting the user input into the right type before using it for any kind of arithmetic calculations.

Now Write a program to convert the value in grams (gm) into Kgs.

Answer:

gm = float(input("Enter value in Grams : "))

kg = gm/1000

print(gm,"grams = ",kg," Kgs.")

Output

Enter value in Grams : 8

8.0 grams = 0.008 Kgs.

>>>

Since a new value of 656 has been assigned to the reserved word ‘int’, you cannot use it for typecasting a float value to integer code. This code will generate a TypeError as follows:

Traceback (most recent call last):

File "<pyshell#3>", line 1, in <module>

int(num1)

TypeError: 'int' object is not callable

Share:

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/qty

print(qty,' apples cost ',price,' therefore one apple costs

',value_of_one,'.')

Output:

How many apples do you have? : 50

What is the total cost? : 250

50 apples cost 250.0 therefore one apple costs 5.0 .

>>>

2. The values of variables are inserted inside the string using format() method.

int(input("How many apples do you have? : "))

price = float(input("What is the total cost? : "))

value_of_one = price/qty

print('{} apples cost {} therefore one apple costs

{}.'.format(qty,price,value_of_one))

The first value in the parenthesis, that is, qty is embedded in the first curly bracket. The second value price is embedded in the second curly bracket and the last value, value_of_one is inserted in the last curly bracket.

Output:

How many apples do you have? : 20

What is the total cost? : 800

20 apples cost 800.0 therefore one apple costs 40.0.

>>>

You can change the order in which the variables are inserted in the string by numbering the curly brackets.

3.

qty = int(input("How many apples do you have? : "))

price = float(input("What is the total cost? : "))

value_of_one = price/qty

print('{2} will be the cost of one apple if {0} apple cost

{1}.'.format(qty,price,value_of_one))

Output:

How many apples do you have? : 10

What is the total cost? : 500

50.0 will be the cost of one apple if 10 apple cost 500.0.

>>>

By default whitespace acts as a separator between the arguments to print() function in Python.

print('What would you like to have?')

print('Rice','lentils','veggies','?')

Output:

What would you like to have?

Rice lentils veggies?

If you want any other separator instead of whitespace, you can mention that in the print function using sep parameter as follows:

print('What would you like to have?')

print('Rice','lentils','veggies','?',sep='/')

Output:

What would you like to have?

Rice/lentils/veggies/?

Share:

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 at the input() method shown as follows:

>>>input('Hi there! Do You live in New York?(Yes/No) :')

Here,

[prompt] = 'Hi there! Do You live in New York?(Yes/No) :'

You will notice that the cursor in front of the prompt string keeps blinking till the user enters a response, and hits the enter key. Till that happens the program execution is put to a halt.

The purpose of this method is to take input from the user and assign it to a variable. The following figure displays the user providing an input value ’Yes’ and pressing the enter key.

>>>user_input = input('Hi there! Do You live in New York?(Yes/No) :') 

Hi there! Do You live in New York?(Yes/No) : Yes

>>> print('the user says: ',user_input)

the user says : Yes

>>>

So, in the preceding code, the input() method is used to capture the response of the user which is a Yes and then the response is assigned to a variable by the name user_input,. Look at the print() method. It concatenates the string ‘The user says : ’ with the value stored in user_input variable and prints the value.

Let’s check the type of value obtained from the input() method (in other words, value of user_input).

>>>user_input = input('Hi there! Do You live in New York? (Yes/No) :')

Hi there! Do You live in New York?(Yes/No) :Yes

>>> type(user_input)

<class 'str'>

>>>

You may wonder what’s the need to check the type of user_input when clearly, the user is providing a string as input? You will understand the importance in the next example. 

Let’s try to use input() method to get user input to perform some arithmetic function. Look at the following code :

qty = input("How many apples do you have? : ")

price = input("What is the total cost? : ")

value_of_one = price/qty

print(value_of_one)

The output will be as follows:

How many apples do you have? : 80

What is the total cost? : 160

Also notice the error message: ‘unsupported operand type(s) for /: 'str' and 'str'’. This error was generated when an attempt was made to divide the variable price with the variable qty. The message is conveying that you made an attempt to divide a string variable with another string variable.

This means that although the user entered a numeric value, Python is treating it as a string value. This is because the input function always returns a string value. No matter what value is provided by the user, the input function will always treat it like a string.

So, in order to divide the price by quantity, you must convert both the values into the right data type.

qty = int(input("How many apples do you have? : "))

price = float(input("What is the total cost? : "))

value_of_one = price/qty

print(value_of_one)

The output will be as follows:

How many apples do you have? : 20

What is the total cost? : 256.50

12.825

In the preceding example, look at the expression int(input("How many apples do you have? : ")). This expression typecasts the value received by the user into an integer.







Share:

Monday, June 12, 2023

Date/Time module

Following is the code to retrieve current date and time

import datetime

print(datetime.datetime.today())

Or

import datetime

print(datetime.datetime.now())

Code to display current hour of the day

from datetime import *

print(getattr(datetime.today(),'hour'))

Code to print current local date

In order to display the present local date we can use of today() method which is defined in the date class.

import datetime

date_object = datetime.date.today()

print(date_object)

Output:

2020-07-22

Function that can be used to see which attributes belong to the datetime module

dir(datetime)

Important classes of the datetime module

1. Date

2. Time

3. Datetime

4. Timedelta

Now what would be the output of the following code:

import datetime

date1 = datetime.date(2020, 7, 23)

print(date1)

Well the Answer is:

2020-07-23

The date() function is actually a constructor of the date class, and it take three arguments: Year, month, and date. This code can also be written as:

from datetime import date

date1 = date(2020, 7, 23)

print(date1)

Now try to write the code to display present date. It should be something like:

from datetime import date

date_today = date.today()

print("Today's date is", date_today)

Output:

Today's date is 2020-07-22

What if someone asks you to print today’s month, year and date separately?

You can print using the following code-

from datetime import date

date_today = date.today()

print("Today's day:", date_today.day, end= " ")

print("Today's month:", date_today.month, end= " ")

print("Today's year:", date_today.year)

Run this code and you should get the output as current month, year and date.

Share:

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 choice

print (" 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 range

print ("randomly generate a number between 1 and 10 :

",random.randrange(1, 10))

print("*******************")

#random()

print ("randomly display a float value between 0 and 1 :

",random.random())

print("* * * * * *")

#shuffle elements of a list or a tuple

seq=[1,32,14,65,6,75]

print("shuffle ",seq,"to produce : ",random.shuffle(seq))

#uniform function to generate a random float number between two numbers

print ("randomly display a float value between 65 and 71 :

",random.uniform(65,71))

OUTPUT

Working with Random Choice

select randomly from [8, 3, 5, 2, 1, 90, 45, 23, 12, 54] : 2

*******************

randomly generate a number between 1 and 10 : 8

*******************

randomly display a float value between 0 and 1 :

0.3339711273144338

* * * * * *

shuffle [1, 32, 14, 75, 65, 6] to produce : None

randomly display a float value between 65 and 71 :

65.9247420528493 

Share:

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 radians

print ("atan(0) : ",math.atan(0))

print("**************")

# cosine of x

print ("cos(90) : ",math.cos(0))

print("**************")

# calculate hypotenuse

print ("hypot(3,6) : ",math.hypot(3,6))

print("**************")

# calculates sine of x

print ("sin(0) : ", math.sin(0))

print("**************")

# calculates tangent of x

print ("tan(0) : ",math.tan(0))

print("**************")

# converts radians to degree

print ("degrees(0.45) : ",math.degrees(0.45))

print("**************")

# converts degrees to radians

print ("radians(0) : ",math.radians(0))

OUTPUT

atan(0) : 0.0

**************

cos(90) : 1.0

**************

hypot(3,6) : 6.708203932499369

**************

sin(0) : 0.0

**************

tan(0) : 0.0

**************

degrees(0.45) : 25.783100780887047

**************

radians(0) : 0.0

>>>

Share:

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 value

a = -52.3

print ("math ceil for ",a, " is : ", math.ceil(a))

print("********************")

#exponential value

a = 2

print("exponential value for ", a, " is: ",math.exp(2))

print("********************")

#absolute value of x

a = -98.4

print ("absolute value of ",a," is: ",abs(a))

print("********************")

#floor values

a = -98.4

print ("floor value for ",a," is: ", math.floor(a))

print("********************")

# log(x)

a = 10

print ("log value for ",a," is : ", math.log(a))

print("********************")

# log10(x)

a = 56

print ("log to the base 10 for ",a," is : ",math.log10(a))

print("********************")

# to the power of

a = 2

b = 3

print (a," to the power of ",b," is : ",math.pow(2,3))

print("********************")

# square root

a = 2

print("sqaure root")

print ("Square root of ",a," is : ", math.sqrt(25))

print("********************")

OUTPUT

math ceil for -52.3 is : -52

********************

exponential value for 2 is: 7.38905609893065

********************

absolute value of -98.4 is: 98.4

********************

floor value for -98.4 is: -99

********************

log value for 10 is : 2.302585092994046

********************

log to the base 10 for 56 is : 1.7481880270062005

********************

2 to the power of 3 is : 8.0

********************

sqaure root

Square root of 2 is : 5.0

********************

Share: