In [ ]:
import numpy as np
from astropy import units as u
In [ ]:
s = 'spam'
s,len(s),s[0],s[0:2]
In [ ]:
s[::-1]
In [ ]:
s = 'spam'
e = "eggs"
s + e
In [ ]:
s + " " + e
In [ ]:
4 * (s + " ") + e
In [ ]:
print(4 * (s + " ") + s + " and\n" + e) # use \n to get a newline with the print function
In [ ]:
"spam" == "good"
In [ ]:
"spam" != "good"
In [ ]:
"spam" == "spam"
In [ ]:
"sp" < "spam"
In [ ]:
"spam" < "eggs"
Unicode charactersYou can enter unicode characters directly from the keyboard (depends on your operating system), or you can use the ASCII encoding.
A list of ASCII encoding can be found here.
For example the ASCII ecoding for the greek capital omega is U+03A9, so you can create the character with \U000003A9
In [ ]:
print("This resistor has a value of 100 k\U000003A9")
In [ ]:
Ω = 1e3
Ω + np.pi
In [ ]:
radio_active = "\U00002622"
wink = "\U0001F609"
print(radio_active + wink)
In [ ]:
☢ = 2.345
☢ ** 2
In [ ]:
n = 4
print("I would like " + n + " orders of spam")
In [ ]:
print("I would like " + str(n) + " orders of spam")
In [ ]:
A = 42
B = 1.23456
C = 1.23456e10
D = 'Forty Two'
In [ ]:
"I like the number {0:d}".format(A)
In [ ]:
"I like the number {0:s}".format(D)
In [ ]:
"The number {0:f} is fine, but not a cool as {1:d}".format(B,A)
In [ ]:
"The number {0:.3f} is fine, but not a cool as {1:d}".format(C,A) # 3 places after decimal
In [ ]:
"The number {0:.3e} is fine, but not a cool as {1:d}".format(C,A) # sci notation
In [ ]:
"{0:g} and {1:g} are the same format but different results".format(B,C)
In [ ]:
"Representation of the number {1:s} - dec: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(A,D)
In [ ]:
NH_D = 34.47 * u.AU
In [ ]:
"The New Horizons spacecraft is {0:.1f} from the Sun".format(NH_D)
In [ ]:
"The New Horizons spacecraft is at a distance of {0.value:.1f} in the units of {0.unit:s} from the Sun".format(NH_D)
In [ ]:
from astropy.table import QTable
In [ ]:
planet_table = QTable.read('Planets.csv', format='ascii.csv')
In [ ]:
for Idx,Val in enumerate(planet_table['Name']):
a = planet_table['a'][Idx] * u.AU
if (a < 3.0 * u.AU):
Place = "Inner"
else:
Place = "Outer"
S = "The planet {0:s}, at a distance of {1:.1f}, is in the {2:s} solar system".format(Val,a,Place)
print(S)
In [ ]:
line = "My hovercraft is full of eels"
In [ ]:
line.replace('eels', 'wheels')
In [ ]:
line.center(100)
In [ ]:
line.ljust(100)
In [ ]:
line.rjust(100, "*")
In [ ]:
line2 = " My hovercraft is full of eels "
In [ ]:
line2.strip()
In [ ]:
line3 = "*$*$*$*$*$*$*$*$My hovercraft is full of eels*$*$*$*$"
In [ ]:
line3.strip('*$')
In [ ]:
line3.lstrip('*$'), line3.rstrip('*$')
In [ ]:
line.split()
In [ ]:
'_*_'.join(line.split())
In [ ]:
' '.join(line.split()[::-1])
In [ ]:
anotherline = "mY hoVErCRaft iS fUlL oF eEELS"
In [ ]:
anotherline.upper()
In [ ]:
anotherline.lower()
In [ ]:
anotherline.title()
In [ ]:
anotherline.capitalize()
In [ ]:
anotherline.swapcase()
In [ ]:
import re
In [ ]:
myline = "This is a test, this in only a test"
In [ ]:
print(myline)
Raw strings begin with a special prefix (r) and signal Python not to interpret backslashes and other special metacharacters in the string, allowing you to pass them through directly to the regular expression engine.
In [ ]:
regex1 = r"test"
In [ ]:
match1 = re.search(regex1, myline)
In [ ]:
match1
In [ ]:
myline[10:14]
In [ ]:
match3 = re.findall(regex1, myline)
In [ ]:
match3
In [ ]:
mynewline = re.sub(regex1, "*TEST*", myline)
In [ ]:
mynewline
In [ ]:
golf_file = open("golf_00").read().splitlines()
In [ ]:
golf_file
In [ ]:
for i in golf_file:
print(i)
In [ ]:
def regex_test_list(mylist, myregex):
for line in mylist:
mytest = re.search(myregex, line)
if (mytest):
print(line + " YES")
else:
print(line + " NOPE")
In [ ]:
regex = r"one"
In [ ]:
regex_test_list(golf_file, regex)
In [ ]:
regex = r"t|n"
In [ ]:
regex_test_list(golf_file, regex)
os package allows you to do operating system stuff without worrying about what system you are using
In [ ]:
import os
In [ ]:
os.chdir("./MyData")
In [ ]:
my_data_dir = os.listdir()
In [ ]:
my_data_dir
In [ ]:
for file in my_data_dir:
if file.endswith(".txt"):
print(file)
In [ ]:
for file in my_data_dir:
if file.endswith(".txt"):
print(os.path.abspath(file))
glob
In [ ]:
import glob
In [ ]:
my_files = glob.glob('02_*.fits')
In [ ]:
my_files
In [ ]:
for file in my_files:
file_size = os.stat(file).st_size
out_string = "The file {0} as a size of {1} bytes".format(file,file_size)
print(out_string)
In [ ]: