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
>>>
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
May 27, 2013
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 '
>>>
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
------------------------
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
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
Subscribe to:
Posts (Atom)