Python Data Types - shahzade baujiti

Breaking

Wednesday, April 24, 2019

Python Data Types

Python Data Types

Python has five standard Data Types :

Numbers
String
List
Tuple
Dictionary


Tip :Python sets the variable type based on the value that is assigned to it :
var = "Python is Awesome"
print(var)
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


1) Numbers : Python supports four types of numbers - int,long,float and complex


Type Format Description
int X=200 Signed Integer
long X=1370L (L) Long integers,they can also be represented in octal and
float X=69.740 (.) Floating point real values
complex X=4+21j Complex Number

A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit.

To define an integer, use the following syntax:

myInt = 6
print myInt
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


To define a floating point number, you may use one of the following notations:
myFloat = 6.0
myFloat = float(6)
print myFloat
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


2) String :
String variables are enclosed with quotes.
Python uses single quotes '' double quotes "" and triple quotes """

Program illustration on String :
firstName = 'Asish'
lastName = "Samantaray"
app = """Python Tutorial"""
print firstName
print lastName
print app
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


3) Lists :
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets []. To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

Program illustraion on lists in Python:

list = [ 'python', 619 , 3.141, True ]
tinylist = [143, 'Asish']

print list          # Prints complete list
print list[0]       # Prints first element of the list
print list[1:3]     # Prints elements starting from 2nd till 3rd
print list[2:]      # Prints elements starting from 3rd element
print tinylist * 2  # Prints list two times
print list + tinylist # Prints concatenated lists
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


4) Tuple :
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets [] and their elements and size can be changed, while tuples are enclosed in parentheses () and cannot be updated. Tuples can be thought of as read-only  lists.Program illustraion on Tuple in Python:


tuple = ( 'python', 619 , 3.141, True )

print tuple          # Prints complete tuple
print tuple[0]       # Prints first element of the tuple
print tuple[1:3]     # Prints elements starting from 2nd till 3rd
print tuple[2:]      # Prints elements starting from 3rd element
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


4) Dictionary :
This is a very powerful datatype to hold a lot of related information, Dictionaries in Python are lists of "Key":"Value" pairs.values can be extracted by the key name.

Dictionaries are created by using braces {} with pairs separated by a comma ,.

Program illustraion Dictionary :
thisdict = {
  "brand": "Lamborghini",
  "model": "Huracan",
  "year": 1963
}
print(thisdict)
Copy
Note: Copy and Paste Code into Compiler
Open Compiler 


PreviousNext

No comments:

Post a Comment