Summary

This document provides an introduction to the Python programming language. It covers the basics, including features, applications, installation, and getting started with interactive and script modes. It also includes the components of the IDLE Python code editor, such as the title bar, menu bar, and command prompt.

Full Transcript

# Python Programming Language ## Introduction To get the desired output from the computer, you give some instructions to it. A set of instructions is called a program. In simple words, a program is a sequence of instructions that specifies how to perform a computation. These programs help a comput...

# Python Programming Language ## Introduction To get the desired output from the computer, you give some instructions to it. A set of instructions is called a program. In simple words, a program is a sequence of instructions that specifies how to perform a computation. These programs help a computer to perform a wide range of tasks. There are some specific computer languages, which are used to write programs. Some of the popular examples of programming languages are Python, C, C++, LISP, Pascal, Java, PHP, etc. In this chapter, you will learn the basics of the Python programming language. ## Python Python is a simple programming language. It is used to write codes for the computer programs. Python was created by Guido van Rossum when he was working at Centrum Wiskunde & Informatica (CWI), which is a National Research Institute for Mathematics and Computer Science in Netherlands. The language was released in 1991. ### Features of Python Some of the features that make Python so popular are as follows: - It is an easy to learn general-purpose high-level programming language. - It is a platform independent programming language, which means it can be used on any machine and in any operating system. - Python needs to use only a few lines of code to perform complex tasks. - It has a simple syntax. - Python is a case-sensitive language. - It is an interpreted language. - It is free to use and even free for commercial redistribution. ### Applications of Python Programming Python is a popular and easy-to-learn language. It can be used to do the following: - Build a website - Program robots - Develop artificial intelligence based applications - Develop games - Perform scientific computations ### Installing Python Python can be easily installed on your computer by following the given steps: 1. To download Python, visit the site: www.python.org/downloads and click on the Download Python button under 'Download the latest version for Windows'. You will get an .exe file. 2. Double-click on the .exe file. Click on Run or Install Now in the dialog box to start the installation. Keep following the instructions to install Python. ## Getting Started with Python After installing Python, you can start with any of the following two modes: 1. **Interactive mode:** The Interactive mode is the default mode. To launch the Interactive mode, click on Start > Scroll down to Python 3.7 > IDLE (Python 3.7). The IDLE screen appears as shown in Figure 4.2. 2. **Script mode:** For writing lengthy programs in Python, the Script mode is used. Using this mode, you can create and edit Python programs. In this mode, you can also save your files so that they can be used later. The complete script is written in an editor. ## Components of IDLE Python Just like any other application window, the IDLE Python screen has the following components: - **Title Bar:** This is the area that displays the name of the application and the document. It also contains command buttons, like Minimize, Maximize/ Restore, and Close. - **Menu Bar:** The menu bar consists of various menus with different options. The menus of the Python window are File, Edit, Shell, Debug, Options, Window, and Help. - **File Menu:** This menu is used to work with the options related to the currently used file. It includes sub-options like opening a new file, opening an existing file, viewing recently used files, saving the files, closing the files, and exiting the application. - **Edit Menu:** This menu is generally used to edit the file in use. You can cut, copy, or paste the selected text. You can also search for a particular text or phrase, and even replace it with some other text. - **Help Menu:** To get help related to any topic of Python, press the F1 key or click on the Help menu. - **Command Prompt:** The command prompt is denoted by '>>>'. This is the area where you type a code. - **Status Bar:** The Status bar tells you the current status of your control or cursor on the window. ## Working in Interactive Mode In the Interactive mode of Python, the instructions are executed, line-by-line, giving the output. In this mode, the commands are typed next to the Python Command prompt (>>>). For example, if you type 3 + 6 next to the Python prompt, it will give you the result as 9. **Note:** The Interactive mode is useful for testing code where you type the commands, one at a time, and get the result or error immediately. ## Printing a Message The print() function is used to display the output of any command on the screen. It can also be used to print the specified messages. For example: ```python >>>print("Hello World") ``` will display **Hello World** **Note:** Python is a case-sensitive programing language. This means that Python differentiates between capital and small letters. For example, print (P capital) and print (p small) are two different things for Python, where print is a valid command in Python, while print is just a word and not a command. You can also pass more than one argument to the print() function. In such a case, the arguments are separated by commas, as shown in Figure 4.7. ## Using IDLE as a Calculator To evaluate an arithmetic expression, it is not necessary to use the print() function. If you type an arithmetic expression at the Python command prompt and press the Enter key, the interpreter automatically evaluates it and shows the result. In this case, the interpreter acts as a simple calculator. The expressions are evaluated using operators like +, -, \*, /, etc. The basic BODMAS rules are followed for performing calculations. ## Variables in Python If you need to store some food for future use, what will you require? Well, you will need a container to store the food. Similarly, when you are working with values in Python, you require some storage location to hold the values for later use. Named storage locations in the computer memory, which are used to store data, are called variables. A variable can store only one data value at a time. When a new value is stored in a variable, its previous value gets overwritten. ### Assigning Values to Variables Values are assigned to variables using the assignment operator (=). For example, the statement x=25 assigns the value 25 to the variable x. ### Rules to Write the Variable Names - A variable name must start with an alphabet (capital or small) or an underscore (_). - A variable name can consist of letters, digits, and underscore. No other character is allowed. - A keyword cannot be used as a variable name. - A variable name can be of any length. - Variable names are case-sensitive (e.g., Age and age are different variable names). ### Examples of Valid and Invalid Variable Names | Invalid Variable Name | Reason | | :----------------------- | :---------- | | Employee code | Space is not allowed in a variable name. | | 10code | Variable name cannot start with a digit. | | A.B.C. Ltd | (.) is not allowed in a variable name. | | ABC@ltd | Special characters are not allowed in a variable name. | | else | 'else' is a keyword in Python, so it cannot be used as a variable name. | ### Using Variables in a Program If a variable is used without being assigned a value, i.e. if the variable is not defined, it gives an error. The following code gives an error because the variable x is used in the print() function without being defined. ## Creating a Module/Script/Program File To start with the Script mode for creating a module or program, follow the given steps: 1. Open IDLE Python Shell, i.e. the Interactive mode. 2. Click on File > New File in IDLE Python Shell as shown in Figure 4.10. 3. In the new window that opens, type the commands you want to save as a program as shown in Figure 4.11. 4. Click on File > Save, and save the file with the name 'prog1.py' where .py is the extension for Python files. ## Running a Module/Script/Program File After the Python program file is created, you can run the program by following the given steps: 1. Open IDLE Python Shell. 2. Click on File > Open. (If the file is already open, you can directly follow the next step). 3. Click on Run > Run Module or press the F5 key. It will execute all the commands. 4. The output will be generated in the Python Shell window. ## Data Types Suppose, you have planned a holiday trip during vacations, and you have to book tickets for your family members. The first action that you need to take is to fill a form to get the reservation done, where you need to give the details like train number, date of journey, name, age, etc. Here, every piece of data that you enter is of a specific type; as your name is a string of letters, age is a numeric type, and date of journey is a date. Similarly, you have to specify in a program, what kind of values will be stored in a specific variable. ## Basic Data Types in Python In Python, a data type represents the type of data stored in a variable. The data stored in memory can be of many types. Python has six standard data types: **Numeric:** - **Integer(int):** It contains positive or negative whole numbers (without fraction or decimal). - **Float:** It is a real number with floating point representation. It is specified by a decimal point. Some examples of floating point numbers are 3.14, -48.6, 18.0, etc. - **Complex Number:** **Sequence Type:** - **String(str):** A string is a collection of one or more characters put in a single quote, double-quote, or triple quote. In python, there is no character data type, a character is a string of length one. It is represented by str class. Examples of strings are "Hello", "Myname", "218", "Peace", etc. - **List:** - **Tuple:** **Other Data Types:** - **Dictionary:** - **Boolean:** True and False with 'T' and 'F' are valid Boolean values otherwise Python will show an error. - **Set:** ## Using input() function The input() function is used to accept the value for a variable from the user. To input integer and float values, you can use int() or float() along with the input() function. ## Using print() Function The print() function is used to print a message or value. - **Conversion:** It converts the message or an object into a string before writing it on the screen. - **Parameters:** It can have multiple parameters. - **Escape Sequences:** It supports multiple escape sequences to format the output. For example, '\n' (new line), '\t' (tab space), and '\r' (carriage return). ### Using Separators with print() Function - **',' operator:** When you use ',' operator as a separator among the values, the values are displayed with a space between them. - **'\t' Escape Sequence:** When you use '\t' escape sequence as a separator among the values, the values are displayed with a tab space between them. - **'\n' Character:** A newline character '\n' in Python is used to end a line and start a new line. In Python, the new line character can be used with the input() function and with the print() function. ## Recap - Python is a high-level language used to write codes for the computer programs. - Python was created by Guido van Rossum when he was working at Centrum Wiskunde and Informatica (CWI). - The Interactive mode is useful for testing the code where you type the commands one at a time and get the result or error immediately. - The Script mode is used for writing lengthy programs in Python. - Named storage locations in the computer memory, which are used to store data, are called variables. - The print() function is used to display the output of any command on the screen. - Python has various standard data types based on the types of values stored in the variables. - The input() function is used to accept the value for a variable from the user. ## Brain Developer Fill in the blanks: 1. **Guido van Rossum** created Python. 2. The **F5** key is pressed to execute a Python program. 3. In the interactive mode of Python, the instructions are executed **line by line**. 4. A **variable** is the name of the memory location that stores the data. 5. **Keywords** cannot be used as a variable name.

Use Quizgecko on...
Browser
Browser