• No results found

04_Basic Data Types input and formatted output.pdf

N/A
N/A
Protected

Academic year: 2020

Share "04_Basic Data Types input and formatted output.pdf"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

Basic Data Types input and formatted output

Nihar Ranjan Roy

[email protected], [email protected] Department of Computer Science and Engineering

GD Goenka University, Gurugram

(2)

Outline

1 Core data types

2 int type

3 float type

4 complex type

5 boolean type

6 string type

7 print() function

8 input() function

(3)

Core data types

Python Core Data Type

Core data types supported by Python are:

1 integer

2 float

3 string

4 complex

(4)

int type

Integer number

Combination of positive and negative numbers including zero(0) Example: 10, -20.

Integer literals can be octal or hexadecimal in format.

Octal numbers start with0o i.e. a zero followed by lower or upper

case letter ‘O’ followed by a sequence of digits from 0 to 7

Hexadecimal numbers start with0x i.e. a zero followed by lower or

upper case letter ‘X’ followed by a sequence of digits.

int function- converts a string or a number into a whole number to integer. It removes anything after the decimal point

Example:

(5)

float type

Floating point number

It consists of whole number, decimal point and fractional part. Example 3.141

A floating point number can be written in decimal notation or scientific notation Example:

3.73e1 is equivalent to 37.0

float function: Converts a string into floating point number Example: float(’12.12’)

(6)

complex type

Complex numbers

A complex number is a number that can be expressed in the form ‘a+bj’, where a and b are real numbers and ‘j’ is an imaginary unit

Example: 2+4j

type(2+4j)

<class ‘complex’>

type(4j)

<class ‘complex’>

(7)

boolean type

Boolean Type

In Python Boolean data type is represented as bool.

It can have one of the two valuesTrue or False.

Generally boolean type is used to compare two variables using relational operators.

Example:>>>5 == 5 True

Example:>>>5>8 False

The bool type is a subclass of the int type and True and False are its only instances:

(8)

string type

String Type I

Strings in python can be enclosed in single quotes (’...’) or double quotes (”...”) with the same result.

name=’nihar’ name=“nihar”

name=“““The quick brown fox jumps over the little lazy dog.The quick brown fox jumps over the little lazy dog.”””

strfunction: it is used to convert a number into string

>>> str(12.5)

String concatenation operator (+)

(9)

string type

String Type II

In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes.

print() produces a more readable version of the string.

(10)

string type

String Type III

(11)

string type

String Type IV

Strings can be indexed (subscripted).

There is no separate character type

(12)

print() function

print() function

print()function is used to print/ display contents on the screen.

Syntax for print() function is print(arguments)

The argument of the print function can be a value of any type int,str,float etc.

print() function with an end argument

print("Nihar")

print("Ranjan")

print("Roy")

(13)

input() function

input function

The input() function is used to accept input string from the user. The input value can be assigned to a variable.

Syntax

variable-name=input()

variable-name=input(”Put-your-message-here”)

Example:

name=input("Please enter your name")

print("Hello ",name," Welcome to the world

of python programming")\\

Output:\\

(14)

input() function

input other than string

How to accept an integer number from the user?

No1=int(input("Please enter a number"))

print("Square of ",No1," is =", No1*No1)

Output:\\

(15)

input() function

Problem

(16)

input() function

Solution: Area of circle

#program for calculating area of circle

radius=int(input("Please enter radius of the circle: ")) PI=3.141

area=PI*radius*radius

print("Area of circle is ",area)

Output:

Please enter radius of the circle: 5 Area of circle is 78.525

Note:Modify this program such that we get only 2 digits after decimal

(17)

Format specifier

Format specifier I

format(item, format-specifier)

item is the number or string to be formatted

format-specifier is a string that specifies how the item is formatted

print(”Area of circle is ”,format(area,’.2f’) Output: Please enter radius of the circle: 5

(18)

Format specifier

Format specifier II

format(10.45678,v

>

10

.

2f

v)

Fieldnwidth

nPrecision

ConversionnCode

Fieldnwidth

nPrecision

ConversionnCode

Justificationn(right)

Some of the popular conversion codes are:

‘s’ is for string

‘d’ is for decimal numbers

(19)

Modules in Python

Modules in Python

A module allows you to logically organize your Python code.

A module is a file consisting of Python code,can define functions, classes and variables.

#demonstrate usage of module

# calculate factorial using inbuilt function

import math

no=input("Enter the no ")

print("Factorial is "+str(math.factorial(int(no))))

You can choose to import only parts from a module, by using the from keyword.

from math import pi

(20)

Math module

Some inbuilt functions from math module

In order to use these functions import math

math.ceil(10.45) gives 11

math.floor(10.45) gives 10

math.exp(1) gives 2.718281.... its value ofex

math.sin(x) returns sin of x where x is in radians math.degrees(x) converts x from radians to degrees

math.radians(x) converts x from degree to radians

(21)

Math module

Problem

(22)

Math module

Solution: Hypotenuse of a right angled triangle

#program for calculating Hypotenuse of a right angle triangle

import math # Import math module

base=int(input("Please enter the base: ")) height=int(input("Please enter the height: ")) hypo=math.sqrt(base*base+height*height)

print("Hypontenuse is=",hypo)

Output:

(23)

Exercise

Practice Exercises

1. What will be printed if we write print() statement as

PriNt(”Hello Mr. Python”)

a. Hello Mr. Python

b. Syntax Error

c. Name Error

d. Both 1 and 2

2. Which of the following is a valid input statement?

a. x=input(Enter number:)

b. X=Input(Enter number:)

c. X=input(’Enter number:’)

d. X=Input(’Enter number:’)

References

Related documents

Employability Skills &amp; WHOQOL Global Scores from three Social Sciences Faculties in

In C programming language printf function is used to print the output string float integer octal and hexadecimal values onto one output screen We use printf function with d

X and Y coordinate System (0,0 in upper left corner) Emulator Java: Class Constructor Public Private Void Data Types: int float String Function Android SDK Terms:

Example: For a type 4H float switch, if the distance of the weight from the bottom of the float (A) is 50 cm, the start/alarm height differential is about 12 cmC.

class Particle(shape: particlepy.shape.Shape , position: Tuple[float, float], velocity: Tuple[float, float], delta_radius: float, data: Optional[dict] = None, alive: bool =

An airborne high spectral resolution lidar (HSRL) based on an iodine absorption filter and a high-power frequency-doubled Nd:YAG laser has been developed to measure backscatter

Trust Training Cooperative - Behavioral Health / Mental Health Provider - Training Needs Survey July 2009 13.. beneficial to direct service behavioral health

International Journal of Information Systems and Engineering (online), Volume 2, Issue 1 (April 2014) 5.3 Granular Access Control.. When it comes to access control, the