Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Download 1.25 Mb.
Pdf ko'rish
bet7/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   2   3   4   5   6   7   8   9   10   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Python Datatypes
There are different types of data that you have to use in programming. Some
are simple texts while others are integers or lists. Python supports a diversity
of datatypes. However, you have to designate the datatype when you create
one so that Python can recognize it easily and process it as per your wishes.
In the following section, I will shed light on several data types that I will be
using in various code snippets in the book.
Datatypes, in a brief look, are as under:
#This is Python list


>>> a = ["tomato, potato, garlic, ginger, pumpkin"]
>>> print(a)
['tomato, potato, garlic, ginger, pumpkin']
>>> #This is python frozenset
>>> a = frozenset({"tomato, potato, garlic, ginger, pumpkin"})
>>> print(a)
frozenset({'tomato, potato, garlic, ginger, pumpkin'})
>>> #This is python set
>>> a = {"tomato, potato, garlic, ginger, pumpkin"}
>>> print(a)
{'tomato, potato, garlic, ginger, pumpkin'}
>>> #This is python tuple
>>> a = ("tomato, potato, garlic, ginger, pumpkin")
>>> print(a)
tomato, potato, garlic, ginger, pumpkin
>>> #This is a dictionary
>>> a = {"fruit" : "tomato", "veg: potato", "veg" : "garlic", "veg" : "ginger",
"veg" : "pumpkin"}
SyntaxError: invalid syntax
>>> a = {"fruit" : "tomato", "veg": "potato", "veg" : "garlic", "veg" :
"ginger", "veg" : "pumpkin"}
>>> print(a)
{'fruit': 'tomato', 'veg': 'pumpkin'}
>>> a = {"fruit" : "tomato", "veg: potato", "veg1" : "garlic", "veg2" :
"ginger", "veg3" : "pumpkin"}
SyntaxError: invalid syntax
>>> a = {"fruit" : "tomato", "veg": "potato", "veg1" : "garlic", "veg2" :


"ginger", "veg3" : "pumpkin"}
>>> print(a)
{'fruit': 'tomato', 'veg': 'potato', 'veg1': 'garlic', 'veg2': 'ginger', 'veg3':
'pumpkin'}
In the above I have turned a string into different datatypes. In the dictionary
section of the code, you will see some errors. One error is because of a
missing quote marks while the other error is because of similar dictionary
names. Dictionaries are important as they allow users to store their important
data in the form of pairs. They are used in a number of ways.

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   ...   48




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling