May 27, 2013

Python IF Statement

Python IF Statement

False Values:
None, 0, empty string, empty list, empty dictionary

Boolean False:
True, False
1, 0

Operators:
==, !=, <, <=, >, >=

and
or
not

Important:
Python doesn't have ! operator,

IDLE 2.6.6    
>>> st = 'my non empty string'
>>> if(!st):

SyntaxError: invalid syntax
>>>



>>> if(not st):
 print "Empty String"
else:
 print "Non Empty String"


Non Empty String
>>>


May 18, 2013

Python Strings - III

1)
Pythonic way to copy a string/list
s[:]

IDLE 2.6.6    
>>> s = 'my new string'
>>> s
'my new string'
>>> s1 = s[:]
>>> s1


2)
Python % and Strings

python comes up with printf like % things.

IDLE 2.6.6    
>>> str = " I am learning %s since %d days " % ('python',5 )
>>> str
' I am learning python since 5 days '
>>>


After %, we need to provide tupple.


3).

Python Unicode Strings:

  
IDLE 2.6.6    

>>> str = u' Python Unicode \u018e '
>>> str
u' Python Unicode \u018e '
>>> s = str.encode('utf-8')
>>>
>>> s
' Python Unicode \xc6\x8e '
>>>

Python Strings - II

Python String Part -II
------------------------

String Methods:
-------------------

Difference between method and function is that, method is called on an 'object'
while function is called independant.

some of the methods:

s.upper() : converts to upper-case
s.lower() : converts to lower-case

s.strip() : trims spaces


s.isalpha() : whether the string is alpha numeric or not


IDLE 2.6.6     

>>> s = 'd'
>>> s.isalpha
<built-in method isalpha of str object at 0x0039ECE0>
>>> s.isalpha()
True


>>> s = 'd3('
>>> s.isalpha()
False


>>> s = '3'
>>> s.isdigit
<built-in method isdigit of str object at 0x003AF580>
>>> s.isdigit()
True

May 17, 2013

Python Strings

Python Strings:

1)
Stings can be enclosed by " ( double quotes ) or  ' ( single quotes )
escaping works on both single quotes and double quotes

2)
Also, we can have multi-line strings.
- But for multiline strings, it has to be escaped by \
- Or we can use """ or '''

example:

a = " this is \
multinline string "

a = """ this is
multine string """


3)
String in python are immutable. i.e. once assigned, it can't be changed.
i.e. all manipulative operations are resulted into new string

4)
we can use [] operator to access string characters
starting from 0 index

s = 'hello'
s[1] will be 'e'

len function returns lenght of the string


4).
There is no ++ operator in python


May 16, 2013

Python Modules and Packages

python module
- module __name__
- a module can contain executable statements as well as functional statements
- those are executed only once
- modules has its private symbol table
- modules can import other modules
- if you include __name__ == __main__ -> then, it will execute
- otherwise, module can be imported too

python module search path
- module is first searched into module path
- then, module is searched in sys.path
- sys.path same as PATH variable in shell


what is python compiled file
- byte compiled.
- modification time

python standard modules
- python comes up with standard modules
- that is called python library reference
- dependant of system implementation

python packages:
- module namespace - by using dotted module names i.e. A.B
- import * = must be in __init__.py file  : 

Python: Dynamic and Strongly Typed Language


Python is both dynamically and strongly typed language.

Dynamic:
- i.e. Types are checked at run-time

Strongly:
- i.e. We can't intermingle variables with different type
         Javascript: var b = 'x' + 3, This will assign 'x3' into b
         While in python, you it would throw an exception



More into Dynamic Nature of Language:

When we declare a variable, we usually associate a type with that.
i.e. integer, float

For Example:

int a
float b

In STATICALLY typed language, variable type is fixed through out life cycle of program execution.

Assigning a = 's', ( integer typed variable to string would throw compile time error )

But, in python, it's dynamic nature.

Variable can hold any type of data through-out the life-cycle of program execution.

a = {'key':'value'} # first as dictionary
a = 'a' # now as String


So, during compile-time, type checks are skipped, Although, as its strongly typed language,
we can't intermingle two different type of variables.

It is up-to programmers test-suite to detect type errors.








Python Basics


Why Python ?

 - Efficient High-Level Data-structures.
    - Tupples
    -  Lists
    - Dictionaries
    - Sets


- Simple Object Oriented Approach

- Elegant Syntax
   a = "hi %s" % "Dave"

- Dynamic Typing
  - i.e. type-checks are performed at run time


sys.exit(0)