Chapter 14 Python Fundamentals

This unit covers Python fundamentals. All the codes here are Python codes.

14.1 Set up Environment

14.2 Conda Environment

  • We can create a conda environment using:

$ conda create --name XXX

Please specify the conda environment name XXX on your own.

  • Similarly, when you add the self-defined conda environment to the notebook kernel list:

$ python -m ipykernel install --user --name=XXX

You need to specify the conda environment name XXX.

There are several important things here:

  • You need to install the relevant modules AFTER you activate the conda environment in the terminal.

  • You need to add the kernel name with python -m ipykernel install --user --name=XXX within the conda enviroment as well.

  • In other words, you need to install the module ipykernel in the target conda environment as well.

  • After a few trial-and-errors, I think the best environment setting is that you only add the kernel name (conda environment) to ipykernel within the conda environment. Do not add the conda environment again in your base python environment.

  • What’s even better is to install jupyter in your conda environment (python-notes) and run your notebook from this python-notes as well.

14.3 Data Type

  • String
  • Numbers (Integers and Floats)
  • Data Type Conversion
  • Input
a = 'cats'
b = 'dogs'
print(a + b)
catsdogs
a = 12
b = 13
print(a+b)
25

14.4 Data Structure

  • List
  • Tuple
  • Dictionary
vocab = ['cat', 'dog', 'bird']
word_pos = (('cat','n'),('dog','n'),('bark','v'))
word_freq = {'cat': 3, 'dog': 1, 'bird': 5}
print(vocab)
['cat', 'dog', 'bird']
print(type(vocab))
<class 'list'>
print(type(word_pos))
<class 'tuple'>
print(type(word_freq))
<class 'dict'>

List and Tuple look similar but they differ in one important aspect: List is mutable while Tuple is Immutable.

That is, when a List is created, particular elements of it can be reassigned. Along with this, the entire list can be reassigned. Elements and slices of elements can be deleted from the list. But these changes will not be possible for a Tuple.

14.5 String

vocab1 =  ['cat', 'dog', 'bird']
vocab2 =  ('cat', 'dog', 'bird')
vocab1[0] = 'human'
print(vocab1)
['human', 'dog', 'bird']
vocab2[0] = 'human'
'tuple' object does not support item assignment
w = 'wonderful'
type(w)
<class 'str'>
w[:3]
'won'

In Python, a String functions as a List:

'o' in w
True
w2 = 'book'
w + w2
'wonderfulbook'
' '.join(w)
'w o n d e r f u l'
  • Useful functions for String:
sent = '   This is a sentence example with leading/trailing spaces.   '
sent.capitalize()
'   this is a sentence example with leading/trailing spaces.   '
sent.title()
'   This Is A Sentence Example With Leading/Trailing Spaces.   '
sent.upper()
'   THIS IS A SENTENCE EXAMPLE WITH LEADING/TRAILING SPACES.   '
sent.lower()
'   this is a sentence example with leading/trailing spaces.   '
sent.rstrip()
'   This is a sentence example with leading/trailing spaces.'
sent.lstrip()
'This is a sentence example with leading/trailing spaces.   '
sent.strip()
'This is a sentence example with leading/trailing spaces.'
sent.find('is')
5
sent.replace('is','was')
'   Thwas was a sentence example with leading/trailing spaces.   '
  • String formatting
nwords = 50
textid = 'diary'
'%s has %d words' % (textid.upper(), nwords)
'DIARY has 50 words'

14.6 Control Structure

  • Iteration (for-loop)
for w in vocab:
  print("_" + w + "_")
_cat_
_dog_
_bird_
  • Condition (if-else)
for w in vocab:
  if(w[0]=="c"):
    print(w)
cat

14.7 Function

def greet(name):
  print('Hello, ' + name + ', how are you doing!')

greet(name='Alvin')
Hello, Alvin, how are you doing!
greet(name='Charlie')
Hello, Charlie, how are you doing!

14.8 List Comprehension

[len(w) for w in vocab]
[3, 3, 4]

14.9 Python Scripts

Depending on the editor you use, you may have two types of Python script files:

  • *.py: A simple python script file
  • *.ipynb: A Jupyter Notebook file which needs to be run in Jupyter Lab/Notebook

14.10 Modules

$ pip install PACKAGE_NAME

Import packages/libraries

import re
import os

14.11 Input/Output

with open('temp.txt', 'w') as f:
    f.write('hello world!\n'+'This is a sentence.')
32
    
with open('temp.txt', 'r') as f:
    texts = [l for l in f.readlines()]
print(texts)
['hello world!\n', 'This is a sentence.']
rm temp.txt
  • File and Directory Operation
import os
os.getcwdb()
b'/Users/alvinchen/Library/CloudStorage/Dropbox/NTNU/Programming_Linguistics/Programming_Linguistics_bookdown'
os.unlink()
os.rename()
os.chdir()
os.listdir()
os.getwd()
os.mkdir()
os.rmdir
os.path.exists()
os.path.isfile()
os.path.isdir()