Python Membership Operators

Mohd Mohtashim
Updated on 24-Jan-2020 12:00:24

203 Views

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −Sr.NoOperator & DescriptionExample1inEvaluates to true if it finds a variable in the specified sequence and false otherwise.x in y, here in results in a 1 if x is a member of sequence y.2not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise.x not in y, here not in results in a 1 if x is not a member of sequence y.Example Live Demo#!/usr/bin/python a = 10 b = 20 list ... Read More

Data Type Conversion in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:41:39

9K+ Views

Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.Sr.No.Function & Description1int(x [, base])Converts x to an integer. base specifies the base if x is a string.2long(x [, base] )Converts x to a long integer. base specifies the base if x is a string.3float(x)Converts x to a floating-point number.4complex(real [, imag])Creates a complex number.5str(x)Converts object x to a string representation.6repr(x)Converts object x to ... Read More

Dictionary Data Type in Python

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

14K+ Views

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.ExampleDictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example − Live Demo#!/usr/bin/python dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john', 'code':6734, 'dept': 'sales'} print dict['one'] # Prints value for 'one' key print ... Read More

Tuple Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:31:28

972 Views

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.ExampleThe main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. For example − Live Demo#!/usr/bin/python tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Prints ... Read More

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

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

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

525 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

HTML5 Cross-Browser iframe Post Message Child to Parent

George John
Updated on 24-Jan-2020 11:25:46

208 Views

The parent object provides a reference to the main window from the child.The following is the parent code. The directive below triggers the iFrame to send a message to the parent window every 3 seconds. No initial message from the main window needed!var a= window.addEventListener ? "addEventListener" : "attachEvent";// here a is the event method var b= window[a];// here b is the eventer var c= a== "attachEvent" ? "onmessage" : "message";// here c is the message event // Listen to message from child window b (c, function(e) {    var d= e.message ? "message" : "data";// here d is ... Read More

Multiple Assignments to Single Value in Python

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

622 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.

Advertisements