Scripting >> Python >> Examples >> Variables >> Assigning data to variables

 

Type Example Literals
Int

python 2.x, 3.x


>>> myInteger = 1
>>> type(myInteger)
<type 'int'>

 
long

 

Python 2.7

>>> myLongInteger=1L
>>> type(myLongInteger)
<type 'long'>

Python 3.x

All integers in Python3 are represented as long integers. Hence, there is no separate number type as long

 

 
float

Python 2.7

>>> myFloat = 1.1
>>> type(myFloat)
<type 'float'>

 

Python 3.6

>>> myFloat=1.1
>>> type(myFloat)
<class 'float'>

 
complex

 Python 2.7

>>> myComplex=1.0+2.0j
>>> type(myComplex)
<type 'complex'>

Python 3.6

>>> myComplex=1.0+2.0j
>>> type(myComplex)
<class 'complex'>

 
str >>> myString="Hello"
>>> type(myString)
<type 'str'>

myString = "Hello"
 

using triple double quotes
myString = """Hello, this is a string
that extends to multiple lines"""

using triple single quotes
myString = '''Hello, this is a string
that extends to multiple lines'''

using \ to contrinue to next line
myString = "Hello \
World"

raw strings to ignore escape sequences
>>> myString = r"New line char is \n"
>>> print(myString)
New line char is \n

Escape sequences used in strings

Sequence Meaning
∖∖ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo ASCII character with octal value ooo
\xhh... ASCII character with hex value hh.

 

list

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([])

Python 2.7

>>> myList=[ 'abcd', 786 , 2.23, 'john', 70.2 ]
>>> type(myList)
<type 'list'>

Python 3.6

>>> myList=[ 'abcd', 786 , 2.23, 'john', 70.2 ]
>>> type(myList)
<class 'list'>

 
tuple

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

The main difference 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

Python 2.7

>>> myTuple=( 'abcd', 786 , 2.23, 'john', 70.2  )
>>> type(myTuple)
<type 'tuple'>

Python 3.6

>>> myTuple=( 'abcd', 786 , 2.23, 'john', 70.2  )
>>> type(myTuple)
<class 'tuple'>

 
 dict

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.

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([])

Python 2.7

>>> myDictionary = { 'name' : 'Joe', 'age' : 30, 'dob' : '1/1/1988' }
>>> type(myDictionary)
<type 'dict'>
>>> print myDictionary['name']
Joe

Python 3.6

>>> myDictionary = { 'name' : 'Joe', 'age' : 30, 'dob' : '1/1/1988' }
>>> type(myDictionary)
<class 'dict'>
>>> print(myDictionary['name'])
Joe

 
sets

Use curly braces {} or set() function to create variables containing sets data type

Python 2.3

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print basket
set(['orange', 'pear', 'banana', 'apple'])
>>> type(basket)
<type 'set'>

 

Python 3.6

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)
{'pear', 'orange', 'apple', 'banana'}
>>> type(basket)
<class 'set'>

 
bytearray

The bytearray() method returns a bytearray object which is a mutable (can be modified) sequence of integers in the range 0 <=x < 256.

syntax
bytearray([source[, encoding[, errors]]])

source
string, integer, object, iterable or blank (creates 0 size array)

string as source
>>> myBytearray=bytearray("Hello")
>>> print(myBytearray)
Hello
>>> type(myBytearray)
<type 'bytearray'>

integer array as source
>>> myBytearray=bytearray([72,101,108,108,111])
>>> print(myBytearray)
Hello
>>> type(myBytearray)
<type 'bytearray'>