mostly things won’t work as advertised if you’re not in the right directory
- ‘ls’ lists the contents of your current directory (folder)
- ‘cd’ changes directories (or folders)
- ‘pwd’ print working directory
- ‘cp’ to copy files, ‘mv’ to move (rename) files
- ‘more’, ‘less’, or ‘cat’ so see files from command line (shell)
- ‘pkg list-all | grep xyz’ to look for package xyz on a chrome book
- NOTE: terminal commands are enclosed in single quotes (”).
- NOTE: the unix prompt will be denoted $ here.
- I’m using Ubuntu as my OS (on a GNU/Linux system, try ‘uname -a’)
- I have python 2.7.15rc1 (‘python’ or ‘python2’) and python 3.6.7 (‘python3’)
- ‘sage’ is built on top of python2, so anything you write in python will work in sage.
- python can be interactive, but there’s also ‘ipython’ (‘ipython2’ and ‘ipython3’)
- The examples I’ll give you look really (almost stupidly) easy. They are intended to get you started for reading and writing more complicated programs.
- Most of programming is knowing what can be done and finding the way to do it.
- ‘scratch’ is a good language to start with, since it shows you the basic elements of programming in an intuitive layout.
- The most important elements are variables, branches (if statements) and loops (for, while, do, etc.)
- in a text editor of your choice, put in the following lines: print(“Hello world!”);
- save the file as “hello.py”
- many ways to run this program (sage is like ipython and #3 is not recommended)
- $ python hello.py
- $ ipython In [1]: %run hello In [2]: exit()
- $ python >>> exec(open(‘hello.py’).read()) >>> exit()
- example with variables
- put this in a file, call it “hello2.py” greeting = “Hello”; recipient = “world”; punctuation = “!”; print(“%s %s%s”%(greeting, recipient, punctuation));
- call this one “example_args.py”
import sys;
print(sys.argv); # prints all of the arguments on the cmd line if (len(sys.argv) > 1): print(sys.argv[1]); # prints the first optional argument
- try running ‘python example_args.py’
- try running ‘python example_args.py abc def hij’
- call this one “hello3.py”
- have this program return each argument on a separate line for an arbitrary number of arguments.
- you figure out how to write it
- if I call it with ‘python hello3.py banana’, it should output “Hello banana!”
- if I call it with ‘python hello3.py dog cat mouse’, it should output “Hello dog!” “Hello cat!” “Hello mouse!”
use a “for” loop to print out all the arguments in sys.argv, but NOT the file name of the program
your will need something like this
for kk in range(10): print(kk);
fid = open(‘deleteme.txt’,’w’); fid.write(“hello world”); fid.close();
fid = open(‘deleteme.txt’,’r’); data1 = fid.read(); fid.seek(0); # “rewinds” the file data2 = fid.read().replace(’ ‘,”) # removes spaces from the read-in text. fid.close();
mylist = [1,2,3,’dog’,’cat’] mylist.append(5)
tuple: I use these only as inputs for functions, I avoid them as variables, since they can’t be chamged!
mytuple = (1,2,3,’horse’)
mydictionary = {‘the’:1, ‘and’:1} mydictionary.update( {‘for’:1} ) mydictionary[‘and’] += 1