Python Programming – Python Data Types and Input Output
The general purpose of the programming language is to process certain kinds of data consisting of numbers, characters, and strings and to provide useful information called output. The task of processing data is achieved by executing a set of instructions or statements referred to as a program. These instructions are formed using certain symbols and words according to some inflexible rules known as syntax rules or grammar. Every program instruction must confirm syntax rules and semantics of the language. Python language has its syntax rules and grammar, which must be followed for writing a code. In this chapter, we will discuss the basic building blocks of the language, i.e., keywords, identifiers, variables, data types, and input/output functions.
- Python Syntax Basics
- Python Programming – Python User Defined Functions
- Python Programming – Python Input and Output
Keywords are the reserved words in Python. A keyword can not be used as a variable name, function name, or any other identifier. In Python, keywords are case-sensitive. Keywords are used to define the syntax and structure of the language. There are 33 keywords in Python 3.3. The list of all the keywords is given in Table 2.1. It is to be noted that all the keywords are in lower case except True, False, and None.
Python Programming – Identifiers
The identifier is the name given to entities like class, functions, variables, etc. It helps differing one entity from another. You can say identifiers are the identification of a user-defined variable, function, or class. Python defines certain rules while declaring an identifier, which is as follows:
Rules for Defining Identifiers
- The identifier can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
- An identifier can not start with a digit. For instance, 3rollno is invalid and rollno3 is valid in Python language.
- Keywords can not be used as identifiers.
- Special symbols such as $, &, @, #, %, etc, can not be used as identifiers.
- Unlike C and C++, in Python, identifiers can be of any length.
Python is a case-sensitive language. This means Sum and sum are not the same. It is advisable that identifier naming should make sense. While, f = 1 is valid. Writing flag = 1 would make more sense and it would be easier to determine what it performs when you look at your programming code after a long gap. Multiple words can be separated using an underscore, total salary of the employee. The camel-case style of writing can also be used in which every first letter of the word except the initial word is capitalized without any spaces. For example totalSalary.
Python language allows assigning a single value to several variables simultaneously. For instance,
Here, all three variables are assigned to the same memory location, when an integer object is created with value 1. Multiple objects can also be assigned to multiple variables as shown below:
|a, b, c = 10, 3.5, “Jack”|
In the above statement, an integer object with value 10 and a floating object with value 3.5 are assigned to variables a and b, respectively. The string object “Jack” is assigned to a variable c.
In this chapter, we have learned basic building blocks of Python language such as keywords, identifiers, variables, and the rules for using them. We have also discussed Python documentation, single-line, and multiline comments. After that, we emphasized data types provided by Python languages such as numbers, strings, lists, tuples, sets, dictionaries, and files. All of these data types are explained with appropriate examples. Then, we have discussed interactive input and output functions in Python. How the formatted input and output can be done is illustrated in detail. In the end, the use of the import command is demonstrated that how one module can be called and used in another module.