Book Image

Learn Python in 7 Days

Book Image

Learn Python in 7 Days

Overview of this book

Python is a great language to get started in the world of programming and application development. This book will help you to take your skills to the next level having a good knowledge of the fundamentals of Python. We begin with the absolute foundation, covering the basic syntax, type variables and operators. We'll then move on to concepts like statements, arrays, operators, string processing and I/O handling. You’ll be able to learn how to operate tuples and understand the functions and methods of lists. We’ll help you develop a deep understanding of list and tuples and learn python dictionary. As you progress through the book, you’ll learn about function parameters and how to use control statements with the loop. You’ll further learn how to create modules and packages, storing of data as well as handling errors. We later dive into advanced level concepts such as Python collections and how to use class, methods, objects in python. By the end of this book, you will be able to take your skills to the next level having a good knowledge of the fundamentals of Python.
Table of Contents (18 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
2
Type Variables and Operators

Overview of dictionary


In Python, a dictionary is a sequence of key-value, or item, pairs separated by commas.

Consider the following example:

port = {22: "SSH", 23: "Telnet" , 53: "DNS", 80: "HTTP" }

The port variable refers to a dictionary that contains port numbers as keys and its protocol names as values. 

Consider the following example:

companies = {"IBM": "International Business Machines", "L&T" :"Larsen & Toubro"}

The syntax of a dictionary is as follows:

Dictionary_name = {key: value}

The key-value pair is called an item. The key and value are separated by a colon (:), and each item is separated by a comma (,). The items are enclosed by curly braces ({ }). An empty dictionary can be created just by using curly braces ({ }). Key features of the dictionary are:

  • The key of the dictionary can not be changed
  • A string, int, or float can be used as a key
  • A tuple that does not contain any list can be used as a key
  • Keys are unique
  • Values can be anything, for example, list, string, int, and so...