You are on page 1of 3

PYTHON PROGRAMMING

To fnd what type a value has, the interpreter can tell you
>>> type("hello")
<type 'str'>
>>>
>>> type(3.2)
<type 'float'>
>>>
>>> type(3)
<type 'int'>
>>>
>>> type("3.2")
<type 'str'>
>>>
A variable is a name that refers to a value
>>> message = 'And now for something completely different'
>>> n = 17
>>> pi = 3.1415926535897932
EXAMPLE
>>> message = "Hilao"
>>> num = 10
>>> loose = 10.20
>>>
>>> type(message)
<type 'str'>
>>> type(num)
<type 'int'>
>>>
>>> type(loose)
<type 'float'>
>>>
EXAMPLE
If you type an integer with a leading zero, you might get a confusing
error: And the result is bizzare
>>> zip = 0
>>> zip
0
>>> zip = 01
>>> zip
1
>>> zip = 010
>>> zip
8
>>> zip = 0100
>>> zip
64
>>> zip = 01000
>>> zip
512
>>>
SOLVE IT.... :D
Python 2 has 31 keywords:
and
as
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
from
global
if
import
in
is
lambda
not
or
pass
print
raise
return
try
while
with
yield
In Python 3, exec is no longer a keyword, but nonlocal is.
Operators are special symbols that represent computations like
addition and multipli cation. The values the operator is applied to
are called operands.
In some other languages, ^ is used for exponentiation, but in Python
it is a bitwise operator called XOR
print ("Please give me a number:")
response = raw_input()
number = int(response)
plusTen = number + 10
## print output in no. Of ways
print ("If we add 10 to your number, comma ", plusTen)
print ("If we add 10 to your number, strFunc " + str(plusTen))
print ("If we add 10 to your number, 5 space string %5s" % plusTen)
print ("If we add 10 to your number, formated {0}".format(plusTen))
List of Learned Functions
print(): Prints its parameter to the console.
input() or raw_input(): asks the user for a response, and returns that response. (Note
that in version 3.x raw_input() does not exist and has been replaced by input())
len(): returns the length of a string (number of characters)
str(): returns the string representation of an obect
int(): given a string or number, returns an integer
Note:
!. input and raw_input function accept a string as parameter. "his string #ill be displayed on the
prompt #hile #aiting for the user input.
$. "he difference bet#een the t#o is that raw_input accepts the data coming from the input
device as a ra# string, #hile input accepts the data and evaluates it into python code. "his is
#hy using input as a #ay to get a user string value returns an error because the user needs to
enter strings #ith %uotes.
&t is recommended to use raw_input at all times and use the int function to convert the ra# string into
an integer. "his #ay #e do not have to bother #ith error messages until the error handling chapter and
#ill not make a security vulnerability in your code.

You might also like