Notizen
python basic
Inhalt der Notizen (1)
Seite 1
Basics:
-Avoid _A or __AA as variable name
Strings:
-type(<variable>) : to get the type of the variable
>>> a = 'whatever'
>>> type(a)
<type 'str'>
>>> a = 'whatever'
>>> a[0]
'w'
>>> a[-1]
'r'
- strings are immutable, so we can make changes.
b = a
b is pointing to same object in memory
-
length function
-
>>> a
'whatever you want to do'
>>> len(a)
23
>>>
\n and \t
>>> c = 'c:\widows\new\test\dir'
>>> print c
c:\widows
ew est\dir
>>>
option 1:
use on more \
eg:
>>> c = 'c:\widows\\new\\test\dir'
>>> print c
c:\widows\new\test\dir
>>>
option 2:
use raw string:
>>> c = r'c:\widows\new\test\dir'
>>> print c
c:\widows\new\test\dir
String Methods:
1) strip():
This method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string.
>>> print a
whatever
>>> print a.strip()
whatever
>>>
2) split()
>>> a = '192.168.1.10'
>>> print a.split('.')
['192', '168', '1', '10']
>>>
- output is a list
3) Join method
join a list with a delimiter
- >>> b
['192', '168', '1', '10']
>>> ".".join(b)
'192.168.1.10'
>>>
>>> a
'192.168.1.10'
>>> '192.168' in a
True
Printing
>>> a
'192.168.1.10'
>>> b
'255.255.255.0'
>>> print "router IP is %s and subnet mask is %s" %(a,b)
router IP is 192.168.1.10 and subnet mask is 255.255.255.0
>>>
>>> print "router IP is %s and subnet mask " % a
router IP is 192.168.1.10 and subnet mask
>>>
>>> a
3.14
>>> print "%.2f" % a
3.14
>>> router_name
'rtr1.blr1'
>>> ip_addr
'192.168.1.1'
>>> print "router name is {0} and IP address of router {0} is {1}".format(router_name,ip_addr)
router name is rtr1.blr1 and IP address of router rtr1.blr1 is 192.168.1.1
>>>
Informationen
- Beschreibung
- ACT Year 12 Certificate python Notiz am python basic, erstellt von inga pinga am 07/04/2016.
- Erstellt von
- inga pinga
- Aufrufe
- 0
- Bewertung