Basic Python: What We Have Learned From The Above Simple Program?
Basic Python: What We Have Learned From The Above Simple Program?
Quick start
In [4]:
a=2
b=5
c = a+b
d = a-b
q, r = a/b, a%b # Yes, this is allowed!
# Now, let's print!
print "Hello World!" # We just had to do this, did we not? print "Sum, Differe
nce = ", c, d
print "Quotient and Remainder = ", q, r
Hello World!
Quotient and Remainder = 0 2
Dynamic Typing We don't declare variables and types in advance. (dynamic typing)
In [5]:
In [6]:
a,b,c,d,e
Out[6]:
(5, 5, 1, 0, 7)
In [7]:
Out[7]:
5
Integers
In [8]:
8 ** 3 # Exponentiation
Out[8]:
512
In [9]:
Out[9]:
148861915063630393937915565865597542319871196538013686865769882092
224332785393313521523901432773468042334765921794473108595202225298
76001L
In [10]:
Out[10]:
(0, 2)
Floats
In [11]:
Out[11]:
(10.0, 10.0)
In [12]:
Out[12]:
2.23606797749979
In [13]:
2. / 4. # No longer a quotient.
Out[13]:
0.5
In [14]:
Out[14]:
(1.0, 0.9000000000000004)
In [15]:
Out[15]:
'HelloHelloHello'
Math Module
In [16]:
import math as mp
In [17]:
x = 45*mp.pi/180.0
mp.sin(x)
Out[17]:
0.7071067811865475
In [18]:
Out[18]:
0.7071067811865475
In [19]:
In [20]:
help(mp.sqrt)
sqrt(...)
sqrt(x)
Strings
In [21]:
a = 10000.
a
Out[21]:
10000.0
In [22]:
In [23]:
Out[23]:
"Tom's Laptop"
In [24]:
Out[25]:
'Tom said, "This is my laptop."'
In [26]:
In [27]:
a_alt
Out[27]:
"Tom's Laptop"
In [28]:
In [29]:
b_alt
Out[29]:
In [30]:
In [31]:
long_string
Out[31]:
'Hello World! \nTom said, "This is my laptop!" '
In [32]:
a= "Hello World"
b= "This is my laptop"
print a
print b
Hello World
This is my laptop
String Arithmetic
In [33]:
s1 = "Hello" ; s2 = "World!"
In [34]:
string_sum = s1 + s2
print string_sum
HelloWorld!
In [35]:
string_product = s1*3
print string_product
HelloHelloHello
In [36]:
print s1*3+s2
HelloHelloHelloWorld!
String is a sequence!
In [37]:
a = "Python rocks!"
In [38]:
Out[38]:
('Pto', 'y', ' ')
In [39]:
Out[39]:
In [40]:
Out[40]:
13
Out[41]:
'thon'
In [42]:
a[8:-2] # indices 8,9 ... upto 2nd last but not including it.
Out[42]:
'ock'
In [43]:
Out[43]:
'Pytho'
In [44]:
Out[44]:
'n rocks!'
In [45]:
a[1:6:2],a[1],a[3],a[5] # Indices 1, 3, 5
Out[45]:
('yhn', 'y', 'h', 'n')
In [46]:
Out[46]:
'Pto ok!'
In [47]:
Out[47]:
'!skcor nohtyP'
String Methods
In [48]:
In [49]:
a.title()
Out[49]:
' I Am A String, I Am An Object, I Am Immutable! '
In [50]:
a.split(",")
Out[50]:
In [51]:
Out[51]:
'I am a string, I am an object, I am immutable!'
In [52]:
------------------------------------------------------------------
---------
TypeError Traceback (most recent c
all last)
<ipython-input-53-846d8c97a6ee> in <module>()
2 a = raw_input("Please enter number 1: ")
3 b = raw_input("Please enter number 2: ")
----> 4 c, d = a+b, a-b
5 q, r = a/b, a*b
6 print c,d,q,r
In [54]:
Enter Number 1: 1
Enter Number 2: 2
Addition = 3.000000, Difference = -1.000000
Division = 2.000000, Quotient = 0.500000
In [55]:
Enter Number 1: 1
Enter Number 2: 3
Addition = 4.00, Difference = -2.00
Division = 3.00, Quotient = 0.33
In [56]:
In [57]:
print statement1
In [58]:
print statement2
In [59]:
print statement3
In [60]:
Conditionals
In [61]:
Enter number: 5
5 is odd!
'!='
'>'
'>='
'<'
'<='
'or'
'not'
a = True; b = False
if a:
print "This comes on screen."
if b:
print "This won't come on screen."
Exercise time
1. Write a program to accept 5 numbers and print their median and standard deviation.
1. Write a program which accepts a name from a user and prints the same name with all letters
capitalized. Use iPython to find the correct string method for the same.
2. Create a program which defines a list [1,2,3,4,5,7,8,9,10]. And then it should accept from the
number from the user and check if it exists inside the list. If in the list it should display "Yes,
found it!" or it should display "Sorry, could not find that."
3. Define a function which accepts a number and checks if the number is prime or not. It should
return True if prime else return a False.