Skip to main content

Data types in Python



Data types in Python

A data type represents the type of data stored into a variable or memory. There are five main data types:
  • Numeric
  • String
  • List
  • Tuple
  • Dictionary

Note – Variables are the address in the memory where the values are stored. We can treat variable like a container where you can put or store something.

Numeric Data Type

The numeric data types are used to represent numbers. There are three sub data types:
  • int
  • float
  • complex

Int data type

The int data type represents an integer number. An integer number are a number without any decimal point or fraction part. For example 200, -100, 67, 100000, etc. Integer uses 4 bytes to store a value. It can range from -2147483648 to 2147483647.

Float data type

The float data type represents floating point numbers. A floating point number is a number that contains a decimal point. Like 3.14, 34.45, -45.678, etc. Float uses 8 bytes to store a value. It can range from 2.3E-308 to 1.7E+308.

Complex data type

The complex data type is a number written in the form of a + bj, where a and b is a real number and j is an imaginary number.

String

A string is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters. Computers do not deal with characters, they deal with binary numbers. Characters are stored internally and manipulated as a combination of 0’s and 1’s. this conversion of a character to a number is called encoding, and the reverse process is called decoding.
String is encoded in two ways: ASCII and Unicode.

Special String operators

Assume, string variable a holds ‘Hello’ and variable b holds ‘Python’, then
Operator
Description
Example
+
Concatenation – Adds values on either side of the operator.
a + b will give:
HelloPython.
a + “ “ + b will give Hello Python
*
Repetition – Creates new strings by concatenating multiple copies of the same string.
a * 2 will give:
HelloHello
[ ]
Slice – Gives the character from the given index.
a[1] will give:
e (second character in ‘Hello’)
[ : ]
Range – Gives the characters from the given range.
a[1:4] will give:
Will give ell (Element at position 4 is not included)
in
Membership – Returns true if a character exists in the given string.
H in a will give :
1 or True

List Data Type

List in Python are similar to arrays in C or Java. A list represent a group of elements. The main difference between a list and array is that a list can store different type of elements, but an array can store only one type of elements. Also list can grow dynamically in memory. The size of array is fixed and they cannot grow at runtime. List are represented using square brackets [ ] and the element are written in [ ], separated by commas.
E.g. Listeg = [10,-15,15.67,’ajit’]
We can use the index operator [ ] to access an item in a list. Index start from 0. Means a list having 5 elements will have the index 0 to 4.
Example:-
listeg = ['a', 'b', 'c', 6, 8, 10]
print("All items in list:", listeg)
print("First element",listeg[0])
print("Last element",listeg[5])
print('c' in listeg)
Output:-
All items in list: ['a', 'b', 'c', 6, 8, 10]
First element a
Last element 10
True

Tuple Data Type

A tuple is like a list. The difference between two is that we cannot change the elements of a tuple, once it is assigned whereas in a list, elements can be changed. This represents tuple can be treated as read-only list. The list items are written under square bracket [ ] while, tuples are written under parenthesis bracket ( ).
Example:-
#following statement represents tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple)
#following statement represents list
thistuple1 = ["kiwi"]
thistuple1.append("mango")
print(thistuple1)
Output:-
('apple', 'banana', 'cherry')
['kiwi', 'mango']

Dictionary Data Type

A dictionary is an unordered collection of items  arranged in the form of ‘key:value’ pair. The elements in the dictionary are placed under curly braces { }. An item has a key and its value is separated by a colon.
Example:-
dict = {‘name’:’Ajay’, ‘Adm id’:’192-18’, ‘class’:’IX’}
print(dict)
#if you want to show only name of the student
print(“Name”,dict[‘name’])
#if you want to print admission no and name of the student
print("Addmission No",dict['Adm id']," ","Name",dict['name'])
Output:-
{‘name’:’Ajay’, ‘Adm Id’:’192-18’, ‘class’:’IX’}
Name Ajay
Addmission No 192-18   Name Ajay

Comments

Popular posts from this blog

Q & A Chapter 1 | Internet Basics | Class 10 CBSE | Computer Application

 Q & A of Chapter 1 Internet Basics A. Tick the correct options – 1. (b) TCP/IP 2. (a) Internet Service Provider 3. (a) URL 4. (b) HTTP 5. (a) Bridge 6. (a) home page 7. (d) All of these 8. (b) Domain Naming System 9. (b) Rules 10. (d) ARPANET B. Very Short Answer Type Questions – 1. In the URL http://www.myfirm.co.in/about.html , what do the following components depict? (a) http                                                 (b) in (a) Http represents a type of server and the protocol on the Internet (b) ‘in’ represents the domain extension of the website. 2. In the URL http://www.favmusic.com/contactus.html what does www.favmusic stand for? www.favmusic represents the domain name i.e. favmusic in www. 3. What does FTP stands for? FTP stands for File Transfer Protocol. 4. Differentiate between webpage and home page. A webpage refers to an electronic page usually written in HTML. It may contain text, images, graphics or hyperlinks. Th

Q & A Chapter 2 | Internet Services | Class 10 CBSE | Computer Application

 Q & A of Chapter 2 A. Tick the correct option – 1. (b) Browser 2. (a) surfing 3. (a) downloading 4. (d) FTP 5. (b) e-mail 6. (c) chat 7. (a) @ 8. (c) Acronyms 9. (d) Emoticons 10. (a) newsgroup B. Very short answer questions – 1. Name a few search engines. Which is the most popular one? Some common search engines are Bing, Google, Ask, Yahoo and MSN. The most popular search engine is Google. 2. What is an Acronym of FTP? File Transfer Protocol. 3. What is a newsreader? Give an example. A newsreader is a software tool or utility used to browse through newsgroup postings. A very popular newsreader in use today is Microsoft Outlook Express. 4. Name three sites that support e-mail services. Gmail, Outlook and Yahoo are three sites that support e-mail services. 5. What is the difference between attachments and FTP? Attachments are the files which are send through an e-mail for communication, while in FTP transfer of files takes place between FTP server and FTP clients and no communicat

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 | QBasic | Class 7 CBSE | Computer Application

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 A. Tick the correct option. 1. (d) screen mode 0 2. (b) 640 x 350 3. (d) White 4. (a) Lower-Right 5. (c) PSET 6. (c) Generate a tone for a short duration B. Fill in the blanks. 1. CIRCLE 2. PAINT 3. PLAY 4. Graphic 5. Pixels 6. Box C. True or False. 1. True 2. False 3. False 4. True 5. True 6. True D. Answer the following questions. 1. What is the use of SCREEN statement? The SCREEN statement is used to set the screen mode for displaying graphics. There are many screen modes and each screen modes have their own number which is from SCREEN 1 to SCREEN 13. The various screen modes differ in resolution. 2. What is a pixel? How is it related to resolution? The graphics which is displaying on the computer screen is made up of thousands of dots called pixel. Pixel stands for picture element. The number of pixels in an image determines the resolution of the image. Higher the resolution, cleaner the image will appear. 3. What is the differenc