Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

Similar documents
Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

Chapter 4. Unix Tutorial. Unix Shell

CIS192: Python Programming

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Python Programming Exercises 1

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Introduction to Linux

Getting started with Hugs on Linux

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Getting started with Hugs on Linux

Basics. proper programmers write commands

Senthil Kumaran S

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Accelerating Information Technology Innovation

Lab 1 Introduction to UNIX and C

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Introduction of Linux

15-122: Principles of Imperative Computation

Accelerating Information Technology Innovation

Scripting Languages Course 1. Diana Trandabăț

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

CHE3935. Lecture 1. Introduction to Linux

Table of Contents EVALUATION COPY

CPD for GCSE Computing: Practical Sheet 6 February 14

The Command Line. Matthew Bender. September 10, CMSC Command Line Workshop. Matthew Bender (2015) The Command Line September 10, / 25

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Mills HPC Tutorial Series. Linux Basics I

Python: common syntax

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction to Unix: Fundamental Commands

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Linux File System and Basic Commands

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

Python Tutorial. Day 1

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

Lab 2: Linux/Unix shell

Operating Systems. Copyleft 2005, Binnur Kurt

Some material adapted from Upenn cmpe391 slides and other sources

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Linux Shell Script. J. K. Mandal

9.2 Linux Essentials Exam Objectives

EE516: Embedded Software Project 1. Setting Up Environment for Projects

The current topic: Python. Announcements. Python. Python

Unit 10. Linux Operating System

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

Linux & Shell Programming 2014

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Conditional Expressions and Decision Statements

Introduction to Computational Models Using Python

Introduction to Lab Sessions

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Helpful Tips for Labs. CS140, Spring 2015

Linux at the Command Line Don Johnson of BU IS&T

Introduction to Linux

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

A Brief Introduction to Unix

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Linux

Unix. 1 tblgrant projects 0 Jun 17 15:40 doc1.txt. documents]$ touch doc2.txt documents]$ ls -l total 0

AI Programming CS S-02 Python

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Open up a terminal, make sure you are in your home directory, and run the command.

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Working With Unix. Scott A. Handley* September 15, *Adapted from UNIX introduction material created by Dr. Julian Catchen

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Introduction to UNIX I: Command Line 1 / 21

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

Introduction to Python - Part I CNV Lab

Lab 1 Introduction to UNIX and C

The Pyth Language. Administrivia

Key Differences Between Python and Java

CS CS Tutorial 2 2 Winter 2018

Advanced Linux Commands & Shell Scripting

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Introduction to the Linux Command Line

Introduction to Linux

Command Line Interface The basics

Introduction to Linux

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Introduction to the UNIX command line

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Transcription:

Shell / Python Tutorial CS279 Autumn 2017 Rishi Bedi

Shell (== console, == terminal, == command prompt) You might also hear it called bash, which is the most widely used shell program macos Windows 10+ Linux Launch Terminal Windows Subsystem for Linux Launch Terminal

Getting around in Bash cd Change working directory cd ~/some/path ls List all files in working directory ls -ltrh pwd Print current working directory pwd mkdir Create a new directory mkdir /new/dir cat Dump out the contents of a file cat file.txt less Preview file contents less file.txt Use man pages to find more information about a command: man ls cp Copy a file cp /path/to/old/file /path/to/new/file mv Move a file mv /path/to/old/file /path/to/new/file

You can do a lot in bash In principle, the bash scripting language is a complete programming language It s especially useful for things like... Plumbing (connecting the inputs & outputs of different console programs) System administration Automating simple command line tasks Quickly examining and editing text files I wouldn t use it for Anything else

Running Python from the Shell There are many ways to run Python The most bare-bones is to run the python command in your shell bash-3.2$ python Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> This is a new kind of shell! To avoid confusion, we ll call it the Python interpreter We can t run bash commands in it, but we can execute Python statements Note that we are running Python 2.7, not Python 3

Running a Python Script The interpreter is neat, especially when you re learning Python, but we also want to be able to save programs and run them in their entirety Many ways to write Python programs the easiest is to use any text editor Sublime Text Atom Vim** Save your Python program, we use the.py extension by convention In your bash shell, run this command, filling in the path to your program: python /path/to/your/program.py

Python Fundamentals Python slides adapted from Sam Redmond s CS41 Comments Variables Output Strings Lists Control Flow Functions

Comments In Python # Single line comments start with a '#' In Java / C++ // single line comment """ """ Multiline strings can be written using three "s, and are often used as function and module comments /* multi line comment */ adapted from Sam Redmond s CS41

Variables In Python x = 5 y = x + 7 z = 3.14 name = Rishi 1 == 1 # => True 5 > 10 # => False True and False # => False not False # => True In Java / C++ int x = 5; int y = x + 7; double z = 3.14; String name = Rishi ; // Java string name( Rishi ); // C++ 1 == 1 # => true 5 > 10 # => false true && false # => false!(false) # => true adapted from Sam Redmond s CS41

Output In Python x = 5 print x name = Rishi print name + str(x) In Java / C++ // Java: int x = 5; System.out.println(x); String name = Rishi ; System.out.println(name + x); // C++: int x = 5; cout << x << endl; string name( Rishi ); cout << name << x << endl; adapted from Sam Redmond s CS41

Strings greeting = 'Hello' group = "world" greeting + ' ' + group + '!' # => 'Hello world!' 0 1 2 3 4 5 6 s[0] = p s[4] = e s[7] = BAD! s[0:3] = pro s[4:] = ein s = 'protein' adapted from Sam Redmond s CS41

Lists # Create a new list empty = [] letters = ['a', 'b', 'c', 'd'] numbers = [2, 3, 5] # Lists can contain elements of different types mixed = [4, 5, "seconds"] # Append elements to the end of a list numbers.append(7) # numbers == [2, 3, 5, 7] numbers.append(11) # numbers == [2, 3, 5, 7, 11] adapted from Sam Redmond s CS41

Lists # Access elements at a particular index numbers[0] # => 2 numbers[-1] # => 11 # You can also slice lists - the same rules apply letters[:3] # => ['a', 'b', 'c'] numbers[1:-1] # => [3, 5, 7] There are many more data structures! dicts are like Maps/HashMaps sets are like Sets tuples are immutable lists # Lists really can contain anything - even other lists! x = [letters, numbers] x # => [['a', 'b', 'c', 'd'], [2, 3, 5, 7, 11]] x[0] # => ['a', 'b', 'c', 'd'] x[0][1] # => 'b' x[1][2:] # => [5, 7, 11] adapted from Sam Redmond s CS41

if Statements if some_condition: print 'Some condition holds' elif other_condition: else: or print 'Other condition holds' print 'Neither condition holds' Each condition should evaluate to a boolean Zero or more elifs else is optional Python has no switch statement! Whitespace matters, unlike in C++ or Java! adapted from Sam Redmond s CS41

for loops for item in iterable: do_something(item) for idx in range(0,10): do_something(item) Strings and lists, amongst other things, are iterables [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for idx in range(0,10): for item in iterable: do_something(idx, item) You can nest loops like this however you d like adapted from Sam Redmond s CS41

Functions def fn_name(param1, param2): value = do_something() return value def starts a function definition return is optional if either return or its value are omitted, implicitly returns None Parameters have no explicit types def iseven(num): if num % 2 == 0: return True else: return False mynum = 100 if iseven(mynum): print str(mynum) + is even adapted from Sam Redmond s CS41

Calling Library Functions Many functions are built-in to Python Some are available in the standard installation, but their modules need to be imported In general, look for built-ins / library functions before writing your own Example: square root function import math math.sqrt(25)

More Python Resources Stanford Python (CS41) Codecademy Python Official Documentation LearnXinYminutes