Found 10476 Articles for Python

List Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:30:34

4K+ Views

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.ExampleThe values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is ... Read More

String Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:29:57

3K+ Views

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.ExampleThe plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example − Live Demo#!/usr/bin/python str = 'Hello World!' print str # Prints complete string print str[0] # Prints first character of the string ... Read More

Number Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:29:29

518 Views

Number data types store numeric values. Number objects are created when you assign a value to them. For example −var1 = 1var2 = 10You can also delete the reference to a number object by using the del statement. The syntax of the del statement is −del var1[, var2[, var3[...., varN]]]]You can delete a single object or multiple objects by using the del statement. For example −del vardel var_a, var_bPython supports four different numerical types −int (signed integers)long (long integers, they can also be represented in octal and hexadecimal)float (floating point real values)complex (complex numbers)ExamplesHere are some examples of numbers −intlongfloatcomplex1051924361L0.03.14j1051924361L0.03.14j100-0x19323L15.2015.20-7860122L-21.99.322e-36j0800xDEFABCECBDAECBFBAEl32.3+e18876j-0490535633629843L-90.-.6545+0J-0x260-052318172735L-32.54e1003e+26J0x69-4721885298529L70.2-E124.53e-7jPython ... Read More

Multiple Assignments to Single Value in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:25:29

615 Views

Python allows you to assign a single value to several variables simultaneously. For example −a = b = c = 1Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example −a,b,c = 1,2,"john"Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c.

Multiple Statements in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:30:19

5K+ Views

Multiple Statements on a Single LineThe semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon −import sys; x = 'foo'; sys.stdout.write(x + '')Multiple Statement Groups as SuitesA group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines ... Read More

Quotation in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:20:02

5K+ Views

Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.The triple quotes are used to span the string across multiple lines. For example, all the following are legal −word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences."""

Multi-Line Statements in Python

Alekhya Nagulavancha
Updated on 19-Apr-2023 14:28:41

7K+ Views

In Python, statements are nothing but instructions given to a Python interpreter to understand and carry out. These statements are usually written in a single line of code. But, that does not mean Python does not have a provision to write these statements in multiple lines. There are two types of statements in Python. They are assignment statements and expression statements. Both of them can be broken into multiple lines of statements and Python interpreter will have no problem understanding them. There are various ways to structure these multi-line statements in Python. Some of them include the following − ... Read More

Lines and Indentation in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:18:54

2K+ Views

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −if True: print "True" else: print "False"However, the following block generates an error −if True: print "Answer" print "True" else: print "Answer" print "False"Thus, in Python all the continuous lines indented with same number of spaces would form a block. The following example has various statement blocks −Note − ... Read More

What are Python Identifiers?

Mohd Mohtashim
Updated on 24-Jan-2020 11:10:50

6K+ Views

A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.Here are naming conventions for Python identifiers −Class names start with an uppercase letter. All other identifiers start with a lowercase letter.Starting an identifier with ... Read More

Python Environment Variables

Mohd Mohtashim
Updated on 24-Jan-2020 11:07:59

975 Views

Here are important environment variables, which can be recognized by Python −Sr.No.Variable & Description1PYTHONPATHIt has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.2PYTHONSTARTUPIt contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.3PYTHONCASEOKIt is used in Windows ... Read More

Advertisements