Reserved Words:
and | exec | not |
assert | finally | or |
break | for | pass |
class | from | |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |
Five Standard Data Types:
-
Numbers
-
String
-
List
-
Tuple
-
Dictionary
Python Numbers:
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
Python Strings:
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 6th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
Python List:
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd to 4th
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
Python Tuples:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints complete list
print tuple[0] # Prints first element of the list
print tuple[1:3] # Prints elements starting from 2nd to 4th
print tuple[2:] # Prints elements starting from 3rd element
print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
Python Dictionary:
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
Basic Operators:
+:-:
*:
/:
%:
**:Exponent - Performs exponential (power) calculation on operators
//:Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.
==:
!=
<>:similar to !=
>
<</span>
>=
<=
=
+=
-=
*=
/=
%=
**=
//=
&:Binary AND Operator copies a bit to the result if it exists in both operands.
|:Binary OR Operator copies a bit if it exists in eather operand.
^:Binary XOR Operator copies the bit if it is set in one operand but not both.
~:Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
<<:
>>:
and:
or:
not:
in:
not in:
is:
is not:
Python Operators Precedence:
Operator | Description |
---|---|
** | Exponentiation (raise to the power) |
~ + - | Ccomplement, unary plus and minus (method names for the last two are +@ and -@) |
* / % // | Multiply, divide, modulo and floor division |
+ - | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise 'AND' |
^ | | Bitwise exclusive `OR' and regular `OR' |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += |= &= >>= <<= *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
note or and | Logical operators |