edits
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
|
||||
In this part, we'll start with the absolute basics of Python.
|
||||
|
||||
## Reading
|
||||
|
||||
### What is Python?
|
||||
|
||||
Python is an interpreted high level programming language. It is often classified as a
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
This section discusses the creation of your first program, running the interpreter,
|
||||
and some basic debugging.
|
||||
|
||||
## Reading
|
||||
|
||||
### Running Python
|
||||
|
||||
Python programs run inside an interpreter.
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
This section covers the basics of text manipulation.
|
||||
|
||||
## Reading
|
||||
|
||||
### Representing Text
|
||||
|
||||
String are text literals written in programs with quotes.
|
||||
@@ -28,6 +26,9 @@ Triple quotes capture all text enclosed in multiple lines.
|
||||
|
||||
### String escape codes
|
||||
|
||||
Escape codes are used to represent control characters and characters that can't be easily typed
|
||||
at the keyboard. Here are some common escape codes:
|
||||
|
||||
```
|
||||
'\n' Line feed
|
||||
'\r' Carriage return
|
||||
@@ -36,7 +37,6 @@ Triple quotes capture all text enclosed in multiple lines.
|
||||
'\"' Literal double quote
|
||||
'\\'` Literal backslash
|
||||
```
|
||||
These codes are inspired by C.
|
||||
|
||||
### String Representation
|
||||
|
||||
@@ -45,9 +45,9 @@ Each character represents a raw unicode code point.
|
||||
<!-- TODO: Add Picture of following characters -->
|
||||
|
||||
```python
|
||||
a = '\xf1' # a = 'ñ'
|
||||
b = '\u2200' # b = '∀'
|
||||
c = '\U0001D122' # c = '𝄢'
|
||||
a = '\xf1' # a = 'ñ'
|
||||
b = '\u2200' # b = '∀'
|
||||
c = '\U0001D122' # c = '𝄢'
|
||||
d = '\N{FORALL}' # d = '∀'
|
||||
```
|
||||
|
||||
@@ -114,11 +114,13 @@ Replacing text.
|
||||
|
||||
```python
|
||||
s = 'Hello world'
|
||||
t = s.replace('Hello' , 'Hallo')
|
||||
t = s.replace('Hello' , 'Hallo') # 'Hallo world'
|
||||
```
|
||||
|
||||
**More string methods:**
|
||||
|
||||
Strings have a wide variety of other methods for testing and manipulating the text data:
|
||||
|
||||
```python
|
||||
s.endswith(suffix) # Check if string ends with suffix
|
||||
s.find(t) # First occurrence of t in s
|
||||
@@ -136,11 +138,11 @@ s.split([delim]) # Split string into list of substrings
|
||||
s.startswith(prefix) # Check if string starts with prefix
|
||||
s.strip() # Strip leading/trailing space
|
||||
s.upper() # Convert to upper case
|
||||
```
|
||||
|
||||
### String Mutability
|
||||
|
||||
Strings are "immutable". They are read only.
|
||||
|
||||
Strings are "immutable" or read-only.
|
||||
Once created, the value can't be changed.
|
||||
|
||||
```python
|
||||
@@ -198,7 +200,7 @@ data = text.encode('utf-8') # text -> bytes
|
||||
|
||||
### Raw Strings
|
||||
|
||||
String with uninterpreted backslash.
|
||||
Raw strings are strings with uninterpreted backslash. They are little by prefixing the initial quote with a lowercase "r".
|
||||
|
||||
```python
|
||||
>>> rs = r'c:\newdata\test' # Raw (uninterpreted backslash)
|
||||
@@ -206,12 +208,12 @@ String with uninterpreted backslash.
|
||||
'c:\\newdata\\test'
|
||||
```
|
||||
|
||||
String is the literal text, exactly as typed.
|
||||
The string is the literal text, exactly as typed.
|
||||
This is useful in situations where the backslash has special significance. Example: filename, regular expressions, etc.
|
||||
|
||||
### f-Strings
|
||||
|
||||
String with formatted expression substitution.
|
||||
A string with formatted expression substitution.
|
||||
|
||||
```python
|
||||
>>> name = 'IBM'
|
||||
@@ -226,9 +228,9 @@ String with formatted expression substitution.
|
||||
>>>
|
||||
```
|
||||
|
||||
**Note: Requires Python 3.6 or newer.**
|
||||
**Note: This requires Python 3.6 or newer.**
|
||||
|
||||
## Exercises 1.4
|
||||
## Exercises
|
||||
|
||||
|
||||
In this exercise, we experiment with operations on Python's string type.
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
This section introduces lists, one of Python's basic objects for storing collections of data.
|
||||
|
||||
## Reading
|
||||
|
||||
### Creating a List
|
||||
|
||||
Use square brackets to create a list:
|
||||
@@ -156,7 +154,7 @@ s.sort() # ['bar', 'foo', 'spam']
|
||||
Specifically, lists don't represent vectors/matrices as in MATLAB, Octave, IDL, etc.
|
||||
However, there are some packages to help you with that (e.g. numpy).
|
||||
|
||||
## Exercises 1.5
|
||||
## Exercises
|
||||
|
||||
In this exercise, we experiment with Python's list datatype. In the last exercise, you worked with strings containing stock symbols.
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
This section discusses the basics of working with files.
|
||||
|
||||
## Reading
|
||||
|
||||
### File Input and Output
|
||||
|
||||
Open a file.
|
||||
@@ -81,7 +79,7 @@ with open('outfile', 'wt') as f:
|
||||
...
|
||||
```
|
||||
|
||||
## Exercises 1.6
|
||||
## Exercises
|
||||
|
||||
This exercise depends on a file `Data/portfolio.csv`. The file contains a list of lines with information on a portfolio of stocks.
|
||||
Locate the file and look at its contents:
|
||||
@@ -91,7 +89,7 @@ Locate the file and look at its contents:
|
||||
*Note: Make sure you are running Python in a location where you can access the `portfolio.csv` file.
|
||||
You can find out where Python thinks it's running by doing this:
|
||||
|
||||
```python
|
||||
```pycon
|
||||
>>> import os
|
||||
>>> os.getcwd()
|
||||
'/Users/beazley/Desktop/practical-python' # Output vary
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
As your programs start to get larger, you'll want to get organized. This section
|
||||
introduces functions. Error handling with exceptions is also introduced.
|
||||
|
||||
## Reading
|
||||
|
||||
### Custom Functions
|
||||
|
||||
Use functions for code you want to reuse. Here is a function definition:
|
||||
@@ -100,8 +98,7 @@ Traceback (most recent call last):
|
||||
RuntimeError: What a kerfuffle
|
||||
```
|
||||
|
||||
|
||||
## Exercises 1.7
|
||||
## Exercises
|
||||
|
||||
### (a) Defining a function
|
||||
|
||||
|
||||
49
_layouts/default.html
Normal file
49
_layouts/default.html
Normal file
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="{{ site.lang | default: "en-US" }}">
|
||||
<head>
|
||||
|
||||
{% if site.google_analytics %}
|
||||
<script async src="https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics }}"></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', '{{ site.google_analytics }}');
|
||||
</script>
|
||||
{% endif %}
|
||||
<meta charset="UTF-8">
|
||||
|
||||
{% seo %}
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#157878">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
||||
<link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}">
|
||||
</head>
|
||||
<body>
|
||||
<a id="skip-to-content" href="#content">Skip to the content.</a>
|
||||
|
||||
<header class="page-header" role="banner">
|
||||
<h1 class="project-name">Practical Python Programming</h1>
|
||||
<h2 class="project-tagline">A course by @dabeaz</h2>
|
||||
<!--{% if site.github.is_project_page %}
|
||||
<a href="{{ site.github.repository_url }}" class="btn">View on GitHub</a>
|
||||
{% endif %}
|
||||
-->
|
||||
{% if site.show_downloads %}
|
||||
<a href="{{ site.github.zip_url }}" class="btn">Download .zip</a>
|
||||
<a href="{{ site.github.tar_url }}" class="btn">Download .tar.gz</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
|
||||
<main id="content" class="main-content" role="main">
|
||||
{{ content }}
|
||||
|
||||
<footer class="site-footer">
|
||||
{% if site.github.is_project_page %}
|
||||
<span class="site-footer-owner"><a href="{{ site.github.repository_url }}">{{ site.github.repository_name }}</a> is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a>.</span>
|
||||
{% endif %}
|
||||
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a>.</span>
|
||||
</footer>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user