Chapter8 Review Python Data types in Python While

  • Slides: 12
Download presentation
Chapter-8 Review Python

Chapter-8 Review Python

Data types in Python �While writing a Python programs, we need to work with

Data types in Python �While writing a Python programs, we need to work with different types of data. �For example , if we have to enter the height of a person, we will be required to use the decimal value, but if we have to write the name of a person, then we need to use a string or a collection of characters. �The type of data value that can be stored in a variable, is known as data type. �The data stored in memory can be of many types.

Data types in Python �The data type of a value or a variable is

Data types in Python �The data type of a value or a variable is an attribute that tells what kind of data a variable can have. �Data types help in classifying different types of data values used in a program. �Python has various standard data types based on the type of value. �For example, a student's marks are stored as a numeric value, whereas, his or her address is stored as alphanumeric characters.

Data types in Python � The main � (Integer): data types used in Python

Data types in Python � The main � (Integer): data types used in Python are as follows: Represents integral numbers (numbers without any fractional part). There are three types of integers in Python: Integer type Description Integer (int) Stores values in the range of - 2147483648 to 2147483647. Long Integers (long int) Support the values that lie beyond the range of integers. Boolean (bool) It represents logical values in the form of True and False. In boolean, 0 represents False and 1 represents True.

Data types in Python �float: Represents floating point values (numbers with fractional part). �

Data types in Python �float: Represents floating point values (numbers with fractional part). � The fractional part of a floating point number may be 0 as well. � Examples of the floating point numbers are, 3. 14, 48. 6, 18. 0, etc. � str (String): A String data type represents strings of characters enclosed within single or double quotation marks (' ' Or ""). � Examples of strings are, 'Hello', "Myname", '218', 'Peace', etc.

Data types in Python �Some examples of data types in Python are: S. no.

Data types in Python �Some examples of data types in Python are: S. no. Value Type 1 1500, -1986 int 2 709, 789, 17. 234 float 3 423. 0 int 4 ‘Python_Language’ str 5 124<987 bool

Type () function �If you want to know what type of data you are

Type () function �If you want to know what type of data you are working with, use the type() function. �It is used to return the data type of a value. Example Print the data type of the variable x: �x = 20 20 print(x) <class 'int'> print(type(x))

Type () function �x = str("Hello World") print(x) print(type(x)) Hello World <class 'str'> �x

Type () function �x = str("Hello World") print(x) print(type(x)) Hello World <class 'str'> �x = float(20. 5) print(x) print(type(x)) 20. 5 <class 'float'> �x = True print(x) print(type(x)) True <class 'bool'>

Input() Function �Input() function is used to accept the value for a variable from

Input() Function �Input() function is used to accept the value for a variable from the user. � To input integer and float values, we can use int() or float() along with input(). �In Python, input() function takes one string argument. �This means that whatever value is being entered by the user, it will be taken as a string argument.

Input() Function Example �Ask for the user's name and print it: a=input('Enter your name:

Input() Function Example �Ask for the user's name and print it: a=input('Enter your name: ') Enter your name: rhea print (a) �val = input("Enter your value: ") Enter your value: �Print (val) 78

Assignment Q 1. Define data types in Python. Q 2. Name the different types

Assignment Q 1. Define data types in Python. Q 2. Name the different types of data types used in Python. Q 3. The following code would print the data type of x, what data type would that be? a) x = 62 print(type(x)) b) x = “Delhi Public School Guwahati" print(type(x)) c) x = 80. 5 print(type(x)) x = False print(type(x)) d)

Assignment e) x=(89 >10) print (x) print(type(x)) Q 4 Write the output of the

Assignment e) x=(89 >10) print (x) print(type(x)) Q 4 Write the output of the following code a) b=input('Enter the first number: ') c=input('Enter the second number: ') print('the sum of the numbers is', b+c) b) b=int(input('Enter the first number: ')) c=int(input('Enter the second number: ')) print('the sum of the numbers is', b+c)