Python Programming - Data Type Conversion

Python Programming – Data Type Conversion

Python Programming – Data Type Conversion While developing a program, sometimes it is desirable to convert one data type into another. In Python, this can be accomplished very easily by making use of built-in type conversion functions. The type conversion functions result in a new object representing the converted value. A list of data type conversion functions with their respective description is given in Table 2.2. Python Programming – Built-in Functions Python Programming – Numbers Convert a list containing float numbers to string in Python Function Description int(n [,base]) Converts n to an integer, base specifies the base if n is a string. long(n [,base]) Converts n to a long integer, base specifies the base if n is a string. float(n) Converts n to a floating-point number. Complex ( real [,imag]) Creates a complex number. str(n) Converts object n to a string representation. repr(n) Converts object n to an expression string. eval(str) Evaluates a string and returns an object. tuple(x) Converts x to a tuple. list(x) Converts x to a list. set(x) Converts x to a set. dict(d) Creates’ a dictionary, d must be a sequence of (key, value) tuples. frozenset(x) Converts x to a frozen set. chr(n) Converts an…

Python Programming - Python Documentation

Python Programming – Python Documentation

Python Programming – Python Documentation Documentation is a very important part of any programming language. The documentation section helps in keeping track of what is a particular line of code or a block of code is performing. It includes the details such as the purpose of the program, author details, important logical aspects, and date of creation. The statements within this section are called comment lines in Python language. In Python language, we use the hash (#) symbol to start writing a comment. If extends up to the newline character. Comments are meant for programmers for a better understanding of a program. Python interpreter ignores the comment. For instance, consider code 2,4. coded in interactive mode programming i.e., directly on the Python interpreter. Python Programming – Understanding Data Type Python Programming – Python User Defined Functions Python Programming – Python Input and Output Code: 2.4. Illustration of Python documentation using # >>>str = “Hello Python” >>> type(str) <class ‘str’> >>> print(str) # It will print the complete string Hello Python >>> str[0] # It will print the value at 0 index ‘H’ >>>str[2:5] # it will print 3<sup>rd</sup> to 5<sup>th</sup> character llo >>>str[2:] # it will print 3rd to the…

Python Programming - Indentation

Python Programming – Indentation

Python Programming – Indentation The majority of the programming languages such as C, C++, and Java use braces {} to define a block of code, whereas, the Python language uses indentation. The block of a code starts with the first unindented line. The amount of indentation is up to the programmer. Moreover, it must be consistent throughout that block. Generally, a tab of 5 white spaces is preferred. For instance, consider code 2.1., Code: 2.1. Illustration of indentation in Python for i in range (0 , 10 ) : print ( i ) if i==5: break output 0 1 2 3 4 5 Python Programming – Exception Handling Python Programming – Decision Making Using Python’s Tabnanny Module for Cleaner Code | The Tabnanny Module in Python with Example indentation also makes the code more readable. Another instance is given in Code 2.2. as follows: Code 2.2. Illustration of indentation in python if True: print (‘Hello’) a=5 output Hello Incorrect indentation will result in IndentationError as displayed in Fig. 2.1. We can see that code 2.2.results in Hello whereas while making incorrect indentation an error occurs “expected an indented block” for the code given in Code 2.3. Code: 23. Illustration of…

Python Programming - Setting up Path

Python Programming – Setting up Path

Python Programming – Setting up Path Many directories contain programs and other executable files, therefore, the operating system Windows, Unix/Linux, or MAC search for executables by providing a search path for directories. The path is a named string maintained by the operating system and is stored in an environment variable. The environment variable contains information available to the come shell and other programs. The Unix is a case-sensitive operating system. However, Windows does not follow case sensitivity. So path variable is named PATH in Unix or Path in windows. On the other hand, in Mac OS, all the path details are handled by the installer. The path variable of your operating system is needed to be set before invoking the python interpreter from any particular directory. How to Run a Python Script via a File or the Shell Using the Python Interpreter Python’s OS Module Setting up the path at Unix/Linux Since many shells are available in Unix/Linux OS, the path set up is slightly different for different shells. For csh, bash, sh, and ksh shells the Python path can be set as follows for a particular session in Unix: In the csh shell: type setenv PATH “$PATH:/usr/local/bin/python” and press…

Python Programming – Literals

Python Programming – Literals

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Literals Literals Literals can be defined as data that is given in a variable or constant. For example 7, -2.9, “XYZ”, ‘Hello’ are literals. Literals are fixed numeric or non-numeric values. Python supports the following literals: String Literals Strings are the only literal sequence type that contains, a sequence of characters. However, characters are not a type. So strings are the lowest-level primitive for character storage and manipulation. String literals are formed by enclosing text in the single or double or both a single and double quote. For example, “Abhishek”, ‘3784’, ‘Some “quoted” \’extra\’ text.’. A string literal can also span multiple lines. It can also be enclosed in matching groups of three single or double quotes (generally referred to as triple-quoted string). For example, ” ” ” This string spans several lines because it is a little long. ” ” ” Python Programming – Python Character Set Python Programming – Token Python Programming – Constants Numeric Literals Python supports three types of numeric literals inte-ger, float and complex. For example, a = 7 #Integer literal b = 25.7 #Float Literal…

Python Programming – Identifiers

Python Programming – Identifiers

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Identifiers An identifier is a unique name that enables you to identify something. Identifiers are used to label variables, functions, classes, objects, and modules. They begin with either a letter or an underscore, and they can contain letters, underscores, or digits. This implies that a single identifier cannot contain any space. They cannot contain punctuation marks. Python is a case-sensitive programming language. Thus AGE, Age, and age are three different identifiers in Python. Some legal identifiers are x, age, marks2, and Total_Marks. Variables are examples of identifiers. Identifiers are. names were given to identify something. Python Programming – Python Character Set Python Programming – Keywords/Reserved Words Python Programming – Python Style Rules and Conventions Points to be Remembered While using Identifiers The following points should be kept in mind while using identifiers: (a) Allowable characters in identifiers are the letters from A to Z, a to z, and the digits from 0 to 9. Identifiers must begin with a letter or under¬score (_) but not with a digit in Python. (b) The identifier must not be a keyword of Python. (c)…

Python Programming - Conditional Statements

Python Programming – Conditional Statements

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Conditional Statements When the program uses a condition to decide which set of statements is to be executed between two alternative set of statements is called conditional statements. For example, “If it is cold then put your coat on.” In the above statement, the resultant action is taken if the condition is evaluated to be true (i.e., the weather is cold). The conditions could be multiple, as in the following conversation: “Ok then, if I come back early from work, I will see you tonight; else if it is too late I will make it tomorrow; else if my brother arrives tomorrow we can get together on Tuesday; else if Tuesday is a holiday, then let it be Wednesday; else I will call you to arrange for the next meeting 1”. Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing Introduction to Python Programming – Pseudocode Python Programming – How To Start Python Python programs can handle such chained conditions as long as you write the appropriate code. In Python, there are many control structures that are used to handle…

Python Programming – Using Comments

Python Programming – Using Comments

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Using Comments Comments are entries in a computer program for the purpose of documentation or explanation. They should be written in such a way that even a begin¬ner can understand the logic of the program easily. Any text contained within comments is ignored by the Python interpreter. Every programming language has different ways to give comments in programs. Comments should be used intelligently to improve the quality and understandability of the program. Python Programming – Introduction to Python Interpreter and Program Execution Python Programming – IDLE Menus Introduction to Python Programming – Interpreter In Python, the line after # (hash) is called a comment. Use # to comment out a line of code or to add a short, one-line comment to your program. For example, # Compute the average of the values avg = sum / number The above line here is a comment that explains what the statement that follows is supposed to do. The comment begins with the # symbol and continues until the end of that line. The interpreter will ignore the # symbol and the contents…

Python Programming – Work Effectively With IDLE

Python Programming – Work Effectively With IDLE

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Work Effectively With IDLE IDLE has lots of features, but you need to know about only a few of them. TAB Completion Start typing in some code, and then press the TAB key. IDLE will offer suggestions to help you complete your statement. Type pr and then TAB at the >>> prompt to see IDLE list of command completion suggestions. Python Programming – How To Start Python Python Programming – How To Download and Install Python Python Programming – Creating a Simple “Hello World” Program Recall Code Statements Press Alt-P to recall the previous code statement entered into IDLE, or press Alt-N to move to the next code statement (assuming there is one). Both key combinations can be used to cycle rapidly through all of the code you have entered into IDLE, re-executing any code statements as needed. Edit Recalled Code Once you recall your code statement, you can edit it and move around the statement using the arrow keys. It’s possible to edit any statement that you have previously entered, even code statements that span multiple lines.

Introduction to Python Programming – Flowcharts

Introduction to Python Programming – Flowcharts

You can learn about Introduction to Programming Using Python Programs with Outputs helped you to understand the language better. Introduction to Python Programming – Flowcharts Flowcharts A flowchart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flowchart symbols are linked together with arrows showing the flow of the process. A flowchart is basically a plan to be followed when the program is being written. It acts as a road map for a programmer and guides him on how to go about it from the starting point to the final point while writing the computer program. For a beginner, it is strongly recommended that the flowchart must be drawn first before writing the program to reduce errors and omissions. Moreover, the flowchart is helpful in testing and modifying the program. Guidelines for Drawing a Flowchart (a) There should be only one entry/starting point and one exit point of the flowchart. (b) Use the correct symbol at each stage in the flowchart. (c) The logical flow should clearly be shown using arrows. (d) Use connectors to reduce the number of flow lines.…