Tutorialslot.com

youtube facebook twitter gmail pinterest

Data types in python


->Numbers
->String
->List
->Tuple
->Dictionary

Numbers:
It helps to store numeric values.Changing the values of number data type leads to the formation of new objects.

Example:
n=5
print(n)
Output:
5

Strings:
Strings are created just by enclosing characters within the quotes. Quotes can be single or double quotes.The starting and ending of quotes should be same for representing a character.

Example:
st="tutorialslot"
print(st)

Output:
tutorialslot

List:
List can be written in the form of sequence of values which is separated by commas within square brackets.A special feature about list in python is that,values specified need not be of same time.

Example:
list1 = ['python', 'java', php, 2016]
list2 = [1, 2, 3, 4, 5, 6, 7 ]

print ("list1[2]: ", list1[2])
print ("list2[1:5]: ", list2[1:5])

Output:
list1[2]: php
list2[1:5]: [2, 3, 4, 5]

Tuple:
In python tuples can be defines as a sequence of constant objects.They are same as list but the difference is that list can be changed but tuples can't and list uses square brackets while the tuples are provided within parenthesis.

Example:
tup1 = ['python', 'java', php, 2016]
tup2 = [1, 2, 3, 4, 5, 6, 7 ]

print ("tup1[2]: ", tup1[2])
print ("tup2[1:5]: ", tup2[1:5])

Output:
tup1[2]: php
tup2[1:5]: [2, 3, 4, 5]

Dictionary:
It uses key for defining values or objects.Values may be same but keys are unique in dictionary. value and key are separated from each oher by using colon(:).

Example:
dict = {'Name': 'tutorial', 'Age': 27, 'Class': 'First'}

print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
When the above code is executed, it produces the following result -

dict['Name']: tutorial
dict['Age']: 27

Comments in python:
The Hash(#) character is used at the beginning of comments and it can be used even at termination of line.

# comment 1
print ("Hello, Python!") # comment 2



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

python python tutorial learn python learning python python code learning java python language python documentation python tutor python basics python download python online python for dummies how to learn python