I have been dabbling in Python for a couple of days now, read the online docs/tutorial and started playing with Python a bit as well…..and I feel like I am a “BIG ZERO” in python.Most everything I see is generally encapsulated in a single script or so large and unwieldy I don’t know where to start. As the first step i downloaded some tutorials and i am publishing my notes here,hoping that someone like me will find them useful..
2 ways of using python to run programs
- Using interactive interpreter prompt
- Using a source file
Using interactive interpreter prompt
Type python at terminal and then press enter
eg:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> print ‘Hello world’
Hello world
>>>
<<< signs are the prompt for entering Python statements.
To Exit press Ctrl+d
Using a source file
First select an editor.
In linux use vim/emacs
eg:-helloworld.py
#!/usr/bin/python
# Filename : helloworld.py
print ‘Hello World’
Run using python helloworld.py
In IDLE use Edit->Run script or Ctrl+F5
Commenting using #
Note–“#!/usr/bin/python”is called shebang line–It tells system that this program should be run with this interpreter when you execute the program.
Executable python programs
Give executable permission using chod and then run
eg:
chmod a+x helloworld.py (a+x for giving permissions to all users)
./helloworld.py
Output–Hello World
help — to get information about any function or statement in python
help(str)–display help for the str class
help()–To know about help itself
help(‘print’)–Display about print
BASICS
Literal Constants–can use literally eg:5,1.23,9.25E-3 and strings like ‘Its a String’
Numbers–4 Types
- integers–whole numbers eg:2
- long integers–bigger whole numbers
- floating point numbers–eg:3.23,52.3E-4
- complex numbers–eg:-5+4j,1.2-3.4j
Strings
A string is a sequence of characters
using strings in Python:
- Using Single Quotes (‘)–eg: ‘jinu’
- Using Double Quotes (“)–eg: “jinu”
- Using Triple Quotes (”’ or “””)–We can specify multi-line strings using triple quotes.We can use single quotes and double quotes freely within the triple quotes.
eg:”’This is a multiline string.This is the first line.This is the second line.”Whats your name?”I asked.He said “Kiran Jayakumar”.”’ - Escape Sequences–
For eg:- To print ‘What’s your name?’ Use ‘What\’s your name?’
- To print “What’s your name?” Use “What\\’s your name?”
- To print two line strings use newline \n (for tab \t).
- A single backslash \ at the end of the line indicates that the string is continued in the next line, but no newline is added
for eg: “This is the first sentence.\
This is the second sentence.”
output:– “This is the first sentence. This is the second sentence.” - Raw Strings–To specify strings where no special processing such as escape sequences are handled.Use prefix r or R. eg: r”Newlines are indicated by \n.”
- Unicode String–Standard way of writing international text.Use prefix u or U. eg: u”This is a unicoded string.”
- Strings are immutable–By immutable it is meant that once you have created a string you cannot change it.
- String literal concatenation–If we place two string literals side by side,they are automatically concatenated be python. eg:‘What\’s ‘ ‘your name?’is automatically converted into “What\’s your name?”
Variables
–Its values can vary
–it need a method of accessing unlike literal constants.
Identifiers
–Names given to identify something.
Rules
- First character of the identifier must be a letter of the alphabet or an underscore _
- Rest of the identifier name can consist of letters,(upper/lowercase),underscores(_)or digits(0-9)
- Identifier names are case sensitive.
eg: i,_my_name,name_23
Indentation
–Used to determine grouping of statements.
Python Data Types
- Numbers: flt_num = 10.0 int_num = 25
- Strings: my_str = “Dude, why are you using perl?”
- Lists: my_list = (“yo”, 24, “blah”, “go away”)
- Tuples: my_tup = (1, 4, 32, “yoyo”, [‘fo’, ‘moog’])
- Dictionaries: my_dict = {‘a’: 24.5, ‘mo’: ‘fo’, 42: ‘answer’}
- Objects: my_inst = MyClass(‘foo’)
List Methods:
- append(x) – add x to the end of the list
- extend(L) – add all items in sequence L to end of list
- insert(i,x) – insert x at a position i
- remove(x) – remove first item equal to x
- pop([i]) – remove item at position i or end of list
- index(x) – return index of first item equal to x
- count(x) – count occurances of x
- sort() – sort the list
- reverse() – reverse the list
Sequence Operations (s,t sequences):
- x in s – test if s contains
- x not in s – test if s does not contain x
- s + t – sequence concatenation
- s * n, n * s – n shallow copies of s s[i] – ith element of s
- s[i:j] – slice of s len(s) – length of s (number of elements)
- min(s) – minimal element of s
- max(s) – maximal element of s

good