Ex. No. :2
|
IMPLEMENTATION OF BASIC PYTHON PROGRAM
|
Python is a remarkably powerful dynamic programming language that is used in a wide variety of application domains. Python is often compared to Tcl, Perl, Ruby, Scheme or Java. Some of its key distinguishing features include:
- very clear, readable syntax
- strong introspection capabilities
- intuitive object orientation
- natural expression of procedural code
- full modularity, supporting hierarchical packages
- exception-based error handling
- very high level dynamic data types
- extensive standard libraries and third party modules for virtually every task
- extensions and modules easily written in C, C++ (or Java for Jython, or .NET languages for IronPython)
INDENTATION
Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops.
The only delimiter is a colon (:) and the indentation of the code itself.
"Whitespace is significant in Python source code."
No, not in general. Only the indentation level of your statements is significant
Everywhere else, whitespace is not significant and can be used as you like, just like in any other language
Celsius to Farenheit
Convert temperature specified in celsius to farenheit, using Python's interactive prompt.
>>> celsius = 100
>>> farenheit = (9 * celsius / 5) + 32
>>> print farenheit
212
>>>
Strings and slicing
>>> word = "newsworthy"
>>> print len(word)
10
>>> print word[1]
e
>>> print word[0:4]
news
>>> print word[4:10]
worthy
>>>
Writing Python scripts
Write a python script to print "Hello World".
1. Create a file with the following statement.
print "Hello World"
2. Save the script as hello.py. Invoke the python interpreter with hello.py as argument.
$ python hello.py
Hello World
Python script to find the first factorial that has more than 100 digits.
n = 1
fact = 1
while fact < (10 ** 100):
n = n + 1
fact = fact * n
Write a Python script that prints prime numbers less than 20.
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
print n, 'is a prime number'
Fibonacci series
Fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> print fibo.__name__
fibo
>>> myfib = fibo.fib
>>> myfib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo import fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>>
--
Don't ever give up.
Even when it seems impossible,
Something will always
pull you through.
The hardest times get even
worse when you lose hope.
As long as you believe you can do it, You can.
But When you give up,
You lose !
I DONT GIVE UP.....!!!
with regards
prem sasi kumar arivukalanjiam
No comments:
Post a Comment