Monday, 15 June 2009

Beginning Python - Chapter 1 Summary


























IDLE is the Python "Integrated Development Environment".
On Windows, I ran it from "C:\Python26\Lib\idlelib\idle.bat".

Simple Python Examples:-

***Strings***
>>>print "hello, " + "world"
hello world

***Addition***
>>>2+2
4

***Division***
>>>10/3
3

***Power of***
>>>2 ** 3
8

***Variables***
>>>x = 3
>>>print x
3

***Get input***
>>>input("Please enter your age: ")
Please enter your age: 29
29

>>>raw_input("Please enter your name: ")
Please enter your name: Egon
Egon

***Functions***
>>>pow(3, 4)
81

>>> round (7.0/3.0)
2.0

***Modules***
>>> import math
>>> math.floor(32.9)
32.0

>>> from math import sqrt
>>> sqrt(9)
3.0

***Commenting***
>>># print "this is a comment"
>>> print "this is code"
this is code

***Representation***
>>> print (10000L)
10000

>>> print repr(10000L)
10000L

>>> temp = 42
>>> print "The temp. is " + `temp`
The temp. is 42

***Long Strings***
>>> print "hello, \
>>>world!
hello, world!

***Raw Strings***
>>> print "Hello \nWorld"
Hello
World

>>> print "c:\\program files\\python\\libs"
c:\program files\python\libs

***Saving & Executing Python Programs***
Create a new file called "test.py"
Edit with a text editor, and add the following:-

name = raw_input("What is your name? ")
print "Hello, " + name
name = raw_input("")

Save, and double click to run or from CMD - "python test.py".


FUNCTIONDESCRIPTION
abs(number)Returns the absolute value of a number.
cmath.sqrt(number)Returns the Square Root of a number.
float(object)Converts a string or a number to a floating point number.
help()Offers interactive help.
input(prompt)Gets input from the user.
int(object)Converts a string or number to an integer.
long(object)Converts a string or number to a long integer.
math.ceil(number)Returns the ceiling of a number as a float.
math.floor(number)Returns the floor of a number as a float.
math.sqrt(number)Square root, for non-negative numbers.
pow(x, y[, z])x to the power of y (modulo z).
raw_input(prompt)Gets input from the user, as a string.
repr(object)Returns the string representation of a value.
round(number[, ndigits])Rounds a number to a given precision.
str(object)Converts a value to a string.

No comments:

Post a Comment