Intro To Pete Alonzi University of Virginia Library Research Data Services February 14, 2017
Outline Installation Getting our sea legs – terminals, and text editors, and IDEs Math and variables Functions Python Data Types Loops Logic Import Odds and ends
Installation of Python Two Notes Language (python) vs language distribution (anaconda) Python 2 vs Python 3 https://www.continuum.io/downloads
Programming vs Python For this workshop we are not requiring any previous knowledge of python or of computer programming. As a result we will be explaining fundamental concepts of each. Our emphasis is on the python but we will spend time on programming as well.
Brief history of Python - https://www.python.org/ Designed by Guido van Rossum 1991 successor to the ABC language Python 2.0 was released on 16 October 2000 Python 3.0 was released on 3 December 2008
Let’s Open Spyder
Programs – “Hello World!” In programming the classic first program is called “Hello World!”
Math and Variables Python knows math! 1+2 Python uses variables to reference things (for example some math). X = 1+2 The “=“ is special. It doesn’t mean equals to. It means refers to. Variable
Math and Variables Math operations Variable rules + - * / ** % // + - * / ** % // Variable rules Must start with a letter Have a type
Functions Functions are prewritten pieces of code that perform one specific task. Let’s take a look at type(…) Function code name arguments
Programs – “Hello World!” In programming the classic first program is called “Hello World!”
Data Types - Strings A string is a string of characters “apple” “blue42” “iamtheverymodelofamodernmajorgeneral” “7hills” Helper functions: string.lower(), string.upper() And many more, check out the online docs
Indexing fruit = ‘ a p p l e ‘ 0 1 2 3 4 -5 -4 -3 -2 -1
Data Types – Strings - Slicing Reference elements of a string with […:…] [from:up to but not including]
Data Types - Lists Lists are represented: name[…] Contains a list of elements of various types You can even have a list of lists Lots of helper functions Reference with [...] and can slice too! Don’t forget tuples: name(…) and immutable
Data Types - Dictionary Dicts are represented: name{…} Elements are a pair: key:value The keys are strings and values are anything Lots of helper functions
Loops – FOR For loop syntax: for ___ in ___ : The first blank is a new variable The second blank is an iterable eg: for x in range(10): print x
Loops – WHILE While loop syntax: while <boolean>: The boolean is checked every iteration eg: while i<10: print i; i+=1
If statements Normally your code progresses linearly Use an if statement for a fork in the road Syntax: if <boolean>: ______ else: _______ To chain multiples together use elif
Import modules A great feature of python is modularity. Lots of people have developed lots of good modules. A great module is called numpy Eg: numpy.random.randn() But my favorite module is called pandas Use nltk for text manipulation
Import modules A great feature of python is modularity. Lots of people have developed lots of good modules. A great module is called numpy Eg: numpy.random.randn() But my favorite module is called pandas
Comments Every programming language has a way for you to include text to help explain the code. In python you can make a comment two ways. # everything after a # is commented Everything between ’’’ is a comment ‘’’
File input/output (I/O) Reading from a file
File input/output (I/O) Writing to a file