Difference between revisions of "PythonNotes"
(→Check if file exists) |
(→Type casting) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 28: | Line 28: | ||
print int(a) |
print int(a) |
||
print str(a) |
print str(a) |
||
print int('0x1a', 16) // prints 26 |
|||
</pre> |
</pre> |
||
Line 96: | Line 97: | ||
</pre> |
</pre> |
||
⚫ | |||
⚫ | |||
arr = [1, 2, 3, 4, 5] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
== 2009-07-08 == |
== 2009-07-08 == |
||
=== Check if file exists === |
=== Check if file exists === |
||
<pre> |
<pre> |
||
import os |
|||
os.path.exists("/path/to/file") |
os.path.exists("/path/to/file") |
||
</pre> |
</pre> |
||
=== Create directories, including parent directories == |
=== Create directories, including parent directories === |
||
<pre> |
<pre> |
||
import os |
|||
dir = "/path/to/dir" |
dir = "/path/to/dir" |
||
if os.path.exists(dir) == False: |
if os.path.exists(dir) == False: |
||
Line 116: | Line 114: | ||
</pre> |
</pre> |
||
Note: <code>os.makedirs</code> issues error when trying to create directory which already exists. |
Note: <code>os.makedirs</code> issues error when trying to create directory which already exists. |
||
=== Check if path entry is a directory === |
|||
⚫ | |||
import os |
|||
os.path.isdir("/path/to/dir/or/file") |
|||
⚫ | |||
⚫ | |||
<pre> |
|||
# loop 1..10: |
|||
⚫ | |||
⚫ | |||
# loop 3..20: |
|||
for i in range(3, 20): |
|||
print i |
|||
</pre> |
|||
=== Extracting a substring === |
|||
<pre> |
|||
s = "This is a string" |
|||
this = s[0:4] |
|||
isa = s[5:7] |
|||
</pre> |
Latest revision as of 16:59, 5 March 2010
Šeit pierakstu lietas, ko iemācos pa ceļam, apgūstot Python ar Gūgles palīdzību - kad kaut ko vajag izdarīt, meklēju vajadzīgās funkcijas/paņēmienus.
Contents
- 1 2009-06-19
- 1.1 Hello world
- 1.2 Main function
- 1.3 Script execution
- 1.4 Get local timestamp
- 1.5 Type casting
- 1.6 Print with several arguments
- 1.7 Convert decimal to hexadecimal
- 1.8 Get ASCII code of a character
- 1.9 Reading/writing files
- 1.10 Store/load variable to/from file
- 1.11 Replace substring
- 1.12 Split string by delimiter
- 2 2009-06-28
- 3 2009-07-04
- 4 2009-07-08
2009-06-19
Hello world
print "Hello, world";
Main function
if __name__ == '__main__': print "Hello world";
Script execution
python yourscript.py
Get local timestamp
import time print time.time()
Type casting
Type casting functions: int(), str(), list(), tuple(), dict().
a = 1.5 print int(a) print str(a) print int('0x1a', 16) // prints 26
Print with several arguments
Type all arguments to strings, concatenate them:
a = 5 print "this is an int: " + str(a)
Convert decimal to hexadecimal
print hex(67)
Get ASCII code of a character
print ord('A')
Reading/writing files
f = open("myfile.txt", "r") contents = f.read() // read the whole file bytes5 = f.read(5) // read 5 bytes outfile = open("output.txt", "w"); outfile.write("abc def"); f.close() outfile.close()
Store/load variable to/from file
import pickle t = [1, 3, 5] f = open("myfile.txt", "w") pickle.dump(f, t) // ... t = pickle.load(f)
Replace substring
s = "Hello, world!" print s.replace("world", "python");
Split string by delimiter
a = "1;2;3" print a.split(";")
2009-06-28
Base64 encode/decode
import base64 encoded = base64.b64encode('data to be encoded') data = base64.b64decode(encoded)
2009-07-04
Defining array
arr = [1, 2, 3, 4, 5]
2009-07-08
Check if file exists
import os os.path.exists("/path/to/file")
Create directories, including parent directories
import os dir = "/path/to/dir" if os.path.exists(dir) == False: os.makedirs(dir)
Note: os.makedirs
issues error when trying to create directory which already exists.
Check if path entry is a directory
import os os.path.isdir("/path/to/dir/or/file")
Looping over numbers
# loop 1..10: for i in range(10): print i # loop 3..20: for i in range(3, 20): print i
Extracting a substring
s = "This is a string" this = s[0:4] isa = s[5:7]