0% found this document useful (0 votes)
5 views16 pages

Advanced Python notes

The document provides an overview of Python data types, including numbers, strings, lists, tuples, dictionaries, booleans, and sets. It explains how Python handles data types dynamically and provides examples of each type, illustrating their usage and characteristics. The document emphasizes the flexibility of Python in managing different data types and includes code snippets to demonstrate their functionality.

Uploaded by

Anant Kanwale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views16 pages

Advanced Python notes

The document provides an overview of Python data types, including numbers, strings, lists, tuples, dictionaries, booleans, and sets. It explains how Python handles data types dynamically and provides examples of each type, illustrating their usage and characteristics. The document emphasizes the flexibility of Python in managing different data types and includes code snippets to demonstrate their functionality.

Uploaded by

Anant Kanwale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Python Data Types

The Java byte keyword is a primitive data type. It is used to declare variables. It can also be used with methods to retu
hold an 8-bit signed two's complement integer.

Points to remember

o The byte range lies between -128 to 127 (inclusive).

o Its default value is 0.

o It is useful to handle the st

o ream of data from a network or file.

Every value has a datatype, and variables can hold values. Python is a powerfully composed language; consequently, w
characterize the sort of variable while announcing it. The interpreter binds the value implicitly to its type.

1. a = 5

We did not specify the type of the variable a, which has the value five from an integer. The Python interpreter will auto
the variable as an integer.

We can verify the type of the program-used variable thanks to Python. The type() function in Python returns the type

Consider the following illustration when defining and verifying the values of various data types.

1. a=10

2. b="Hi Python"

3. c = 10.5

4. print(type(a))

5. print(type(b))

6. print(type(c))

Output:

<type 'int'>

<type 'str'>

<type 'float'>

Standard data types

A variable can contain a variety of values. On the other hand, a person's id must be stored as an integer, while their na
a string.

The storage method for each of the standard data types that Python provides is specified by Python. The following is a
defined data types.

1. Numbers

2. Sequence Type
3. Boolean

4. Set

5. Dictionary

The data types will be briefly discussed in this tutorial section. We will talk about every single one of them exhaustivel
instructional exercise.

Numbers

Numeric values are stored in numbers. The whole number, float, and complex qualities have a place with a Python Nu
Python offers the type() function to determine a variable's data type. The instance () capability is utilized to check whe
place with a specific class.

When a number is assigned to a variable, Python generates Number objects. For instance,

1. a = 5

2. print("The type of a", type(a))

3.

4. b = 40.5

5. print("The type of b", type(b))

6.

7. c = 1+3j

8. print("The type of c", type(c))


9. print(" c is a complex number", isinstance(1+3j,complex))

Output:

The type of a <class 'int'>

The type of b <class 'float'>

The type of c <class 'complex'>

c is complex number: True

Python supports three kinds of numerical data.

o Int: Whole number worth can be any length, like numbers 10, 2, 29, - 20, - 150, and so on. An integer can be a
Python. Its worth has a place with int.

o Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc. It can be accurate to within 15 decimal plac

o Complex: An intricate number contains an arranged pair, i.e., x + iy, where x and y signify the genuine and non
separately. The complex numbers like 2.14j, 2.0 + 2.3j, etc.

Sequence Type

String

The sequence of characters in the quotation marks can be used to describe the string. A string can be defined in Pytho
double, or triple quotes.

String dealing with Python is a direct undertaking since Python gives worked-in capabilities and administrators to perf

When dealing with strings, the operation "hello"+" python" returns "hello python," and the operator + is used to comb

Because the operation "Python" *2 returns "Python," the operator * is referred to as a repetition operator.

The Python string is demonstrated in the following example.

Example - 1

1. str = "string using double quotes"

2. print(str)

3. s = '''''A multiline

4. string'''

5. print(s)

Output:

string using double quotes

A multiline

string

Look at the following illustration of string handling.


Example - 2

1. str1 = 'hello javatpoint' #string str1

2. str2 = ' how are you' #string str2

3. print (str1[0:2]) #printing first two character using slice operator

4. print (str1[4]) #printing 4th character of the string

5. print (str1*2) #printing the string twice

6. print (str1 + str2) #printing the concatenation of str1 and str2

Output:

he

hello javatpointhello javatpoint

hello javatpoint how are you

List

Lists in Python are like arrays in C, but lists can contain data of different types. The things put away in the rundown are
comma (,) and encased inside square sections [].

To gain access to the list's data, we can use slice [:] operators. Like how they worked with strings, the list is handled by
operator (+) and the repetition operator (*).

Look at the following example.

Example:

1. list1 = [1, "hi", "Python", 2]

2. #Checking type of given list

3. print(type(list1))

4.

5. #Printing the list1

6. print (list1)

7.

8. # List slicing

9. print (list1[3:])

10.

11. # List slicing

12. print (list1[0:2])


13.

14. # List Concatenation using + operator

15. print (list1 + list1)

16.

17. # List repetation using * operator

18. print (list1 * 3)

Output:

[1, 'hi', 'Python', 2]

[2]

[1, 'hi']

[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

Tuple

In many ways, a tuple is like a list. Tuples, like lists, also contain a collection of items from various data types. A parent
separates the tuple's components from one another.

Because we cannot alter the size or value of the items in a tuple, it is a read-only data structure.

Let's look at a straightforward tuple in action.

Example:

1. tup = ("hi", "Python", 2)

2. # Checking type of tup

3. print (type(tup))

4.

5. #Printing the tuple

6. print (tup)

7.

8. # Tuple slicing

9. print (tup[1:])

10. print (tup[0:1])

11.

12. # Tuple concatenation using + operator

13. print (tup + tup)


14.

15. # Tuple repatation using * operator

16. print (tup * 3)

17.

18. # Adding value to tup. It will throw an error.

19. t[2] = "hi"

Output:

<class 'tuple'>

('hi', 'Python', 2)

('Python', 2)

('hi',)

('hi', 'Python', 2, 'hi', 'Python', 2)

('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)

Traceback (most recent call last):

File "main.py", line 14, in <module>

t[2] = "hi";

TypeError: 'tuple' object does not support item assignment

Dictionary

A dictionary is a key-value pair set arranged in any order. It stores a specific value for each key, like an associative array
is any Python object, while the key can hold any primitive data type.

The comma (,) and the curly braces are used to separate the items in the dictionary.

Look at the following example.

1. d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}

2.

3. # Printing dictionary

4. print (d)

5.

6. # Accesing value using keys

7. print("1st name is "+d[1])

8. print("2nd name is "+ d[4])


9.

10. print (d.keys())

11. print (d.values())

Output:

1st name is Jimmy

2nd name is mike

{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}

dict_keys([1, 2, 3, 4])

dict_values(['Jimmy', 'Alex', 'john', 'mike'])

Boolean

True and False are the two default values for the Boolean type. These qualities are utilized to decide the given assertio
The class book indicates this. False can be represented by the 0 or the letter "F," while true can be represented by any

Look at the following example.

1. # Python program to check the boolean type

2. print(type(True))

3. print(type(False))

4. print(false)

Output:

<class 'bool'>

<class 'bool'>

NameError: name 'false' is not defined

Set

The data type's unordered collection is Python Set. It is iterable, mutable(can change after creation), and has remarka
elements of a set have no set order; It might return the element's altered sequence. Either a sequence of elements is
curly braces and separated by a comma to create the set or the built-in function set() is used to create the set. It can c
of values.

Look at the following example.

1. # Creating Empty set

2. set1 = set()

3.

4. set2 = {'James', 2, 3,'Python'}

5.
6. #Printing Set value

7. print(set2)

8.

9. # Adding element to the set

10.

11. set2.add(10)

12. print(set2)

13.

14. #Removing element from the set

15. set2.remove(2)

16. print(set2)

Output:

{3, 'Python', 'James', 2}

{'Python', 'James', 3, 2, 10}

{'Python', 'James', 3, 10}

Next TopicPython Keywords

← prevnext →
Related Posts

Python Multiprocessing

In this article, we will learn how we can achieve multiprocessing using Python. We also discuss its
advanced concepts. What is Multiprocessing? Multiprocessing is the ability of the system to run one
or more processes in parallel. In simple words, multiprocessing uses the two or more CPU within...

9 min read

Write CSV File

Python CSV File A CSV stands for "comma-separated values", which is defined as a simple file format
that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or
database in plain text and has a standard format for data interchange. The CSV...

4 min read

Read CSV File

Python read csv file CSV File A csv stands for "comma separated values", which is defined as a simple
file format that uses specific structuring to arrange tabular data. It stores tabular data such as
spreadsheet or database in plain text and has a common format for data...

5 min read


Python SimpleImputer module

In this tutorial, we are going to learn about the SimpleImputer module of the Sklearn library, and it
was iously known as impute module but updated in the latest versions of the Sklearn library. We will
discuss the SimpleImputer class and how we can use it...

4 min read

Second Largest Number in Python

When we have a lot of elements in our list, the thought of finding the highest or lowest element can
come to our mind and Python has made it much easier for us. In this article, we shall how we can use
to find the second largest...

3 min read

Python Sets

Python Set A Python set is the collection of the unordered items. Each element in the set must be
unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we
can modify it after its creation. Unlike other collections in Python, there is...

13 min read

Python Strings

Python String Till now, we have discussed numbers as the standard data-types in Python. In this
section of the tutorial, we will discuss the most popular data type in Python, i.e., string. Python string
is the collection of the characters surrounded by single quotes, double quotes, or triple...

9 min read

Python JSON

JSON, which stands for JavaScript Object Notation, is a popular data format for online data exchange.
JSON is the best format for organizing data between a client and a server. The programming language
JavaScript is comparable to this language's syntax. JSON's primary goal is data transmission...

6 min read

Python OpenCV object detection

OpenCV is the huge and open-source library for image processing, machine learning and computer
vision. It is also playing an important role in real-time operation. With the help of the OpenCV library,
we can easily process the images as well as videos to identify the objects,...

6 min read

Python Lambda Functions

This tutorial will study anonymous, commonly called lambda functions in Python. A lambda function
can take n number of arguments at a time. But it returns only one argument at a time. We will
understand what they are, how to execute them, and their syntax. What are...

7 min read

Learn Important Tutorial

Python

Java

Javascript

HTML
Database

PHP

C++

React

B.Tech / MCA

DBMS

Data Structures

DAA
Operating System

Computer Network

Compiler Design

Computer Organization

Discrete Mathematics

Ethical Hacking

Computer Graphics
Web Technology

Software Engineering

Cyber Security

Automata

C Programming

C++

Java

.Net
Python

Programs

Control System

Data Warehouse

Preparation

Aptitude

Reasoning

Verbal Ability
Interview Questions

Company Questions

Advertisement

Advertisement

Advertisement

Advertisement

You might also like