Skip to content

Latest commit

 

History

History
107 lines (94 loc) · 3.58 KB

python_notes.org

File metadata and controls

107 lines (94 loc) · 3.58 KB

unix commands to know.

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

general info

  • 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’)

on programming

  • 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.)

first program: hello world

  • 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)
    1. $ python hello.py
    2. $ ipython In [1]: %run hello In [2]: exit()
    3. $ python >>> exec(open(‘hello.py’).read()) >>> exit()

second program: hello world, v2

  • example with variables
  • put this in a file, call it “hello2.py” greeting = “Hello”; recipient = “world”; punctuation = “!”; print(“%s %s%s”%(greeting, recipient, punctuation));

third program: reading command line arguments

  • 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’

fourth program: hello, v3

  • 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!”

loops

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);

writing to file

fid = open(‘deleteme.txt’,’w’); fid.write(“hello world”); fid.close();

reading from a file

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();

containers or iterables

list

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’)

dict

mydictionary = {‘the’:1, ‘and’:1} mydictionary.update( {‘for’:1} ) mydictionary[‘and’] += 1