Data Types - Python Presentation PDF

Summary

This presentation covers various data types in Python, including numbers, strings, lists, tuples, and dictionaries. It explores their characteristics, usages, and demonstrates examples.

Full Transcript

Data Types Sneha George/AP/CSE/KITS Data Types Variables can hold values of different data types. Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type....

Data Types Sneha George/AP/CSE/KITS Data Types Variables can hold values of different data types. Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. Python enables us to check the type of the variable used in the program. Python provides us the type() function which returns the type of the variable passed. Example a=10 b="Hi Python" c = 10.5 print(type(a)) print(type(b)) print(type(c)) OUTPUT: Standard data types Numbers String List Tuple Dictionary Numbers stores numeric values. Python creates Number objects when a number is assigned to a variable. Example: a=3,b=5 Python supports 4 types of numeric data: 1.int (signed integers like 10, 2, 29, etc.) 2.long (long integers used for a higher range of values like 908090800L, -0x1929292L, etc.) 3.float (float is used to store floating point numbers like 1.9, 9.902, 15.2, etc.) 4.complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.) -contains an ordered pair, i.e., x + iy where x and y String can be defined as the sequence of characters represented in the quotation marks. can use single, double, or triple quotes to define a string. the operator + is used to concatenate two strings The operator * is known as repetition operator Example str1 = 'hello Mech' str2 = ” how are you” print (str1[0:2]) print (str1) print (str1*2) print (str1 + str2) OUTPUT he O hello Mechhello Mech hello Mechhow are you List Lists are similar to arrays in C can contain data of different types The items stored in the list are separated with a comma (,) and enclosed within square brackets []. can use slice [:] operators to access the data of the list. Example l = [1, "hi", "python", 2] print (l[3:]) print (l[0:2]) print (l) print (l + l) print (l * 3) OUTPUT [1, 'hi'] [1, 'hi', 'python', 2] [1, 'hi', 'python', 2, 1, 'hi', 'python', 2] [1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2] Tuple is similar to the list in many ways tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses (). is a read-only data structure as we can't modify the size and value of the items of a tuple. Example1 t = ("hi", "python", 2) print (t[1:]) print (t[0:1]) print (t) print (t + t) print (t * 3) print (type(t)) OUTPUT ('python', 2) ('hi',) ('hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2) Example2 t = ("hi", "python", 2) print (t[1:]); print (t[0:1]); print (t); print (t + t); print (t * 3); print (type(t)) t = "hi"; OUTPUT ('python', 2) ('hi',) ('hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2) Traceback (most recent call last): File "main.py", line 16, in t="hi"; Dictionary is an ordered set of a key-value pair of items. like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type whereas value is an arbitrary Python object. The items in the dictionary are separated with the comma and Example d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike '}; print("1st name is "+d); print("2nd name is "+ d); print (d); print (d.keys()); print (d.values()); OUTPUT 1st name is Jimmy 2nd name is mike {1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'} [1, 2, 3, 4] ['Jimmy', 'Alex', 'john', 'mike'] Keywords special reserved words which convey a special meaning to the compiler/interpreter. 33 keywords – Python 3.7 35 keywords – async & await – Python 3.8 Each keyword have a special meaning and a specific operation. These keywords can't be used as variable

Use Quizgecko on...
Browser
Browser