Variables and Operations in Python

✓ Variables

# Variable assignment
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.1415926535897932

#Identifying variable type
>>> type(message)
<type 'str'>
>>> type(n)
<type 'int'>
>>> type(pi)
<type 'float'>

✓ Operators and Operands – The operators +, -, *, / and ** are used to perform addition, subtraction, multiplication, division and exponentiation.

>>> minute = 59
>>> minute/60
0

>>> minute/60.0
0.98333333333333328

✓ Order of Operations

#Parentheses have the highest precedence
>>> 2 * (3-1)
4
>>> (1+1)**(5-2)
8

#Exponentiation has the next highest precedence
>>> 2**1+1
3
>>> 3*1**3
3

#Multiplication and Division have the same precedence, which is higher than
Addition and Subtraction
>>> 2*3-1
5
>>> 6+4/2
8

#Operators with the same precedence are evaluated from left to right (except expo nentiation)
>>> degrees / 2 * pi
>>> degrees / 2 / pi

✓ String Operations

>>> first = 'ahilan'
>>> second = 'kana'
>>> print first + second
ahilankana

>>> 'ahilan'*3 
ahilanahilanahilan 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s