Basic data types in python

1,590 views 25 slides Aug 13, 2021
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

BASIC DATA TYPES IN PYTHON


Slide Content

Basic Data Types in Python PROF. SUNIL D. CHUTE HEAD DEPT OF COMPUTER SCIENCE M.G. COLLEGE ARMORI

Basic Data Types in Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

One way to categorize these basic data types is in one of four groups: Numeric :  int , float and the less frequently encountered complex Sequence :  str  (string), list and tuple Boolean : (True or False) Dictionary :  dict (dictionary) data type, consisting of (key, value) pairs NOTE: I t's important to point out that Python usually doesn't require you to specify what data type you are using and will assign a data type to your variable based on what it thinks you meant. An equally important thing to point out is that Python is a "loosely/weakly typed" programming language, meaning that a variable can change its type over the course of the program's execution, which isn't the case with "strongly typed" programming languages (such as Java or C++).

Numeric Data Types These data types are fairly straight-forward and represent numeric values. These can be decimal values, floating point values or even complex numbers. Integer Data Type - int The  int  data type deals with integers values. This means values like  0, 1, -2 and -15 , and not numbers like  0.5, 1.01, -10.8 , etc. If you give Python the following code, it will conclude that a is an integer and will assign the  int  data type to it: >>> x = 5 >>> type(x) <class ' int '> We could have been more specific and said something along these lines, to make sure Python understood our 5 as an integer, though, it'll automatically do this exact same thing under the hood:

>>> x = int (5) >>> type(x) <class ' int ‘> It's worth noting that Python treats any sequence of numbers (without a prefix) as a decimal number. This sequence, in fact, isn't constrained. That is to say, unlike in some other languages like Java, the value of the  int  doesn't have a maximum value - it's unbounded. The  sys.maxsize  may sound counterintuitive then, since it implies that that's the maximum value of an integer, though, it isn't. >>> x = sys.maxsize >>> x 2147483647 This appears to be a 32-bit signed binary integer value, though, let's see what happens if we assign a higher number to x: >>> x = sys.maxsize >>> x+1 2147483648

In fact, we can even go as far as: >>> y = sys.maxsize + sys.maxsize >>> y 4294967294 The only real limit to how big an integer can be is the memory of the machine you're running Python on. Prefixing Integers What happens when you'd like to pack a numeric value in a different form? You can prefix a sequence of numbers and tell Python to treat them in a different system. More specifically, the prefixes: 0b or 0B - Will turn your integer into Binary 0o or 0O - Will turn your integer into Octal 0x or 0X - Will turn your integer into Hexadecimal

# Decimal value of 5 >>> x = 5 >>> x 5 # Binary value of 1 >>> x = 0b001 >>> x 1 # Octal value of 5 >>> x = 0o5 >>> x 5 # Hexadecimal value of 10 >>> x = 0x10 >>> x 16

Floating Point Data Type - float The float type in Python designates a floating-point number. float values are specified with a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify  scientific notation Ex.1 >>> 4.2 4.2 >>> type(4.2) <class 'float'> Ex.2 >>> 4. 4.0 Ex.3 >>> .2 0.2 Ex.4 >>> .4e7 4000000.0 >>> type(.4e7) <class 'float'> Ex.5 >>> 4.2e-4 0.00042

Complex Numbers Complex numbers are specified as <real part>+<imaginary part>j. Ex. >>> 2+3j (2+3j) >>> type(2+3j) <class 'complex'>

Strings Strings are sequences of character data. The  string type  in Python is called str. String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string: Ex. >>> print("I am a string.") I am a string. >>> type("I am a string.") <class ' str '> Ex. >>> print('I am too.') I am too. >>> type('I am too.') <class ' str '>

A string in Python can contain as many characters as you wish. The only limit is your machine’s memory resources. A string can also be empty: Ex. >>> '' '' What if you want to include a quote character as part of the string itself? Your first impulse might be to try something like this: Ex. >>> print('This string contains a single quote (') character.') SyntaxError : invalid syntax Ex. >>> mystring = "This is not my first String" >>> print ( mystring ); This is not my first String

to join two or more strings. Ex. >>> print ("Hello" + "World"); HelloWorld Ex. >>> s1 = "Name Python " >>> s2 = "had been adapted " >>> s3 = "from Monty Python" >>> print (s1 + s2 + s3) Name Python had been adapted from Monty Python

use input() function Ex. >>> n = input("Number of times you want the text to repeat: ") Number of times you want the text to repeat: >>> print ("Text"*n); TextTextTextTextText Check existence of a character or a sub-string in a string Ex .>>> "won" in "India won the match" True

Python List data type List  is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type. Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ]. Ex. a = [1, 2.2, 'python'] We can use the slicing operator [ ] to extract an item or a range of items from a list. The index starts from 0 in Python.

Python Tuple data type Tuple  is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically. It is defined within parentheses () where items are separated by commas. Ex. t = (5,'program', 1+3j)

We can use the slicing operator [] to extract items but we cannot change its value.

Python Set data type Set  is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered. A set is created by placing all the items (elements) inside curly braces {}, separated by comma, or by using the built-in set() function. It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable elements like  lists , sets or  dictionaries  as its elements.

Python Dictionary data type Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. In Python, dictionaries are defined within braces {} with each item being a pair in the form  key:value . Key and value can be of any type.

Conversion between data types