Veze, linkovi
Kompjuter biblioteka
Tiny Python Projects

Python programiranje Python programiranje

Tiny Python Projects

Broj strana: 440
ISBN broj: 9781617297519
Godina izdanja: 2020.

Twitter   Facebook   Linkedin   Pinterest   Email
Predlog za prevod


Tiny Python Projects is a gentle, amusing introduction to Python that will firm up several key concepts while occasionally making you snicker. Amanda Debler, Schaeffler Technologies A long journey is really a lot of little steps. The same is true when you’re learning Python, so you may as well have some fun along the way! Written in a lighthearted style with entertaining exercises that build powerful skills, Tiny Python Projects takes you from amateur to Pythonista as you create 22 bitesize programs. Each tiny project teaches you a new programming concept, from the basics of lists and strings right through to regular expressions and randomness. Along the way you’ll also discover how testing can make you a better programmer in any language.

About the Technology

Who says learning to program has to be boring? The 21 activities in this book teach Python fundamentals through puzzles and games. Not only will you be entertained with every exercise, but you’ll learn about text manipulation, basic algorithms, and lists and dictionaries as you go. It’s the ideal way for any Python newbie to gain confidence and experience.

About the book

The projects are tiny, but the rewards are big: each chapter in Tiny Python Projects challenges you with a new Python program, including a password creator, a word rhymer, and a Shakespearean insult generator. As you complete these entertaining exercises, you’ll graduate from a Python beginner to a confident programmer—and you’ll have a good time doing it!

Why Write Python?

Who Am I?

Who Are You?

Why Did I Write This Book?

0 Getting Started: Introduction and Installation Guide

0.1 Using test-driven development

0.2 Setting up your environment

0.3 Code examples

0.4 Getting the code

0.5 Installing modules

0.6 Code formatters

0.7 Code linters

0.8 How to start writing new programs

0.9 Why Not Notebooks?

0.10 The scope of topics we cover

0.11 Why not object-oriented programming?

0.12 A Note about the lingo

1 How to write and test a Python program

1.1 Comment lines

1.2 Testing our program

1.3 Adding the shebang line

1.4 Making a program executable

1.5 Understanding $PATH

1.6 Altering your $PATH

1.7 Adding a parameter and help

1.8 Making the argument optional

1.9 Running our tests

1.10 Adding the main() function

1.11 Adding the get_args() function

1.11.1 Checking style and errors

1.12 Testing

1.13 Starting a new program with

1.14 Using as an alternative to

1.15 Summary

2 The Crow’s Nest: Working with strings

2.1 Getting started

2.1.1 How to use the tests

2.1.2 Creating programs with

2.1.3 Write, test, repeat

2.1.4 Defining your arguments

2.1.5 Concatenating strings

2.1.6 Variable types

2.1.7 Getting just part of a string

2.1.8 Finding help in the REPL

2.1.9 String methods

2.1.10 String comparisons

2.1.11 Conditional branching

2.1.12 String formatting

2.2 Solution

2.3 Discussion

2.3.1 Defining the arguments with get_args

2.3.2 The main thing

2.3.3 Classifying the first character of a word

2.3.4 Printing the results

2.3.5 Running the test suite

2.4 Summary

2.5 Going Further

3 Going on a picnic: Working with lists

3.1 Starting the program

3.2 Writing

3.3 Introduction to lists

3.3.1 Adding one element to a list

3.3.2 Adding many elements to a list

3.3.3 Indexing lists

3.3.4 Slicing lists

3.3.5 Finding elements in a list

3.3.6 Removing elements from a list

3.3.7 Sorting and reversing a list

3.3.8 Lists are mutable

3.3.9 Joining a list

3.4 Conditional branching with if/elif/else

3.5 Solution

3.6 Discussion

3.6.1 Defining the arguments

3.6.2 Assigning and sorting the items

3.6.3 Formatting the items

3.6.4 Printing the items

3.7 Summary

3.8 Going Further

4 Jump the Five: Working with dictionaries

4.1 Dictionaries

4.1.1 Creating a dictionary

4.1.2 Accessing dictionary values

4.1.3 Other dictionary methods

4.2 Writing

4.3 Solution

4.4 Discussion

4.4.1 Defining the arguments

4.4.2 Using a dict for encoding

4.4.3 Method 1: Using a for loop to print() each character

4.4.4 Method 2: Using a for loop to build a new string

4.4.5 Method 3: Using a for loop to build a new list

4.4.6 Method 4: Turning a for loop into a list comprehension

4.4.7 Method 5: Using the str.translate() function

4.4.8 (Not) using str.replace()

4.5 Summary

4.6 Going Further

5 Howler: Working with files and STDOUT

5.1 Reading files

5.2 Writing files

5.3 Writing

5.4 Solution

5.5 Discussion

5.5.1 Defining the arguments

5.5.2 Reading input from a file or the command line

5.5.3 Choosing the output file handle

5.5.4 Printing the output

5.5.5 A low-memory version

5.6 Review

5.7 Going Further

6 Words Count: Reading files/STDIN, iterating lists, formatting strings

6.1 Writing

6.1.1 Defining file inputs

6.1.2 Iterating lists

6.1.3 What you’re counting

6.1.4 Formatting your results

6.2 Solution

6.3 Discussion

6.3.1 Defining the arguments

6.3.2 Reading a file using a for loop

6.4 Review

6.5 Going Further

7 Gashlycrumb: Looking items up in a dictionary

7.1 Writing

7.2 Solution

7.3 Discussion

7.3.1 Handling the arguments

7.3.2 Reading the input file

7.3.3 Using a dictionary comprehension

7.3.4 Dictionary lookups

7.4 Review

7.5 Going Further

8 Apples and Bananas: Find and replace

8.1 Altering strings

8.1.1 Using the str.replace() method

8.1.2 Using str.translate()

8.1.3 Other ways to mutate strings

8.2 Solution

8.3 Discussion

8.3.1 Defining the parameters

8.4 Eight ways to replace the vowels

8.4.1 Method 1: Iterate every character

8.4.2 Method 2: Using the str.replace() method

8.4.3 Method 3: Using the str.translate() method

8.4.4 Method 4: List comprehension

8.4.5 Method 5: List comprehension with function

8.4.6 Method 6: The map() function

8.4.7 Method 7: Using map() with a defined function

8.4.8 Method 8: Using regular expressions

8.5 Refactoring with tests

8.6 Review

8.7 Going Further

9 Dial-A-Curse: Generating random insults from lists of words

9.1 Writing

9.1.1 Validating arguments

9.1.2 Importing and seeding the random module

9.1.3 Defining the adjectives and nouns

9.1.4 Taking random samples and choices

9.1.5 Formatting the output

9.2 Solution

9.3 Discussion

9.3.1 Defining the arguments

9.3.2 Using parser.error()

9.3.3 Program exit values and STDERR

9.3.4 Controlling randomness with random.seed()

9.3.5 Iterating for loops with range()

9.3.6 Constructing the insults

9.4 Review

9.5 Going Further

10 Telephone: Randomly mutating strings

10.1 More briefing

10.2 Writing

10.3 Calculating the number of mutations

10.4 The mutation space

10.5 Selecting the characters to mutate

10.5.1 Non-deterministic selection

10.5.2 Randomly sampling characters

10.6 Mutating a string

10.7 Time to write

10.8 Solution

10.9 Discussion

10.9.1 Defining the arguments

10.9.2 Mutating a string

10.9.3 Using a list instead of a str

10.10 Review

10.11 Going Further

11 Bottles of Beer Song: Writing and testing functions

11.1 Writing

11.2 Counting down

11.3 Writing a function

11.4 Writing a test for verse()

11.5 Using the verse() function

11.6 Solution

11.7 Discussion

11.7.1 Defining the arguments

11.7.2 Counting down

11.7.3 Test-Driven Development

11.7.4 The verse() function

11.7.5 Iterating through the verses

11.7.6 1500 other solutions

11.8 Review

11.9 Going Further

12 Ransom: Randomly capitalizing text

12.1 Writing

12.1.1 Mutating the text

12.1.2 Flipping a coin

12.1.3 Creating a new string

12.2 Solution

12.3 Discussion

12.3.1 Iterating through elements in a sequence

12.3.2 Writing a function to choose the letter

12.3.3 Another way to write list.append()

12.3.4 Using a str instead of a list

12.3.5 Using a list comprehension

12.3.6 Using a map() function

12.4 Comparing methods

12.5 Review

12.6 Going Further

13 Twelve Days of Christmas: Algorithm design

13.1 Writing

13.1.1 Counting

13.1.2 Creating the ordinal value

13.1.3 Making the verses

13.1.4 Using the verse() function

13.1.5 Printing

13.1.6 Time to write

13.2 Solution

13.3 Discussion

13.3.1 Defining the arguments

13.3.2 Making one verse

13.3.3 Generating the verses

13.3.4 Printing the verses

13.4 Review

13.5 Going Further

14 Rhymer: Using regular expressions to create rhyming words

14.1 Writing

14.1.1 Breaking a word

14.1.2 Using regular expressions

14.1.3 Using capture groups

14.1.4 Truthiness

14.1.5 Creating the output

14.2 Solution

14.3 Discussion

14.3.1 Stemming a word

14.3.2 Writing a regular expression

14.3.3 Testing and using the stemmer() function

14.3.4 Creating rhyming strings

14.3.5 Writing stemmer() without regular expressions

14.4 Review

14.5 Going Further

15 The Kentucky Friar: More regular expressions

15.1 Writing

15.1.1 Splitting text using regular expressions

15.1.2 Shorthand classes

15.1.3 Negated shorthand classes

15.1.4 Using re.split() with a captured regex

15.1.5 Writing the fry() function

15.1.6 Using the fry() function

15.2 Solution

15.3 Discussion

15.3.1 Breaking text into lines

15.3.2 Writing the fry() function manually

15.3.3 Writing the fry() function with regular expressions

15.4 Review

15.5 Going Further

16 The Scrambler: Randomly reordering the middles of words

16.1 Writing

16.1.1 Breaking the text into lines and words

16.1.2 Capturing, non-capturing, and optional groups

16.1.3 Compiling a regex

16.1.4 Scrambling a word

16.1.5 Scrambling all the words

16.2 Solution

16.3 Discussion

16.3.1 Processing the text

16.3.2 Scrambling a word

16.4 Review

16.5 Going Further

17 Mad Libs: Using regular expressions

17.1 Writing

17.1.1 Using regular expressions to find the pointy bits

17.1.2 Halting and printing errors

17.1.3 Getting the values

17.1.4 Substituting the text

17.2 Solution

17.3 Discussion

17.3.1 Defining the arguments

17.3.2 Substituting with regular expressions

17.3.3 Finding the placeholders without regular expressions

17.4 Review

17.5 Going Further

18 Gematria: Numeric encoding of text using ASCII values

18.1 Writing

18.1.1 Cleaning a word

18.1.2 Ordinal character values and ranges

18.1.3 Summing and reducing

18.1.4 Encoding the words

18.1.5 Breaking the text

18.2 Solution

18.3 Discussion

18.3.1 Writing word2num()

18.3.2 Sorting

18.3.3 Testing

18.4 Review

18.5 Going Further

19 Workout Of the Day: Parsing CSV file, creating text table output

19.1 Writing

19.1.1 Reading delimited text files

19.1.2 Manually reading a CSV file

19.1.3 Parsing with the csv module

19.1.4 Creating a function to read a CSV file

19.1.5 Selecting the exercises

19.1.6 Formatting the output

19.1.7 Handling bad data

19.1.8 Time to write!

19.2 Solution

19.3 Discussion

19.3.1 Reading a CSV file

19.3.2 Potentials run-time errors

19.3.3 Using pandas.read_csv() to parse the file

19.3.4 Formatting the table

19.4 Review

19.5 Going Further

20 Password Strength: Generating a secure and memorable password

20.1 Writing

20.1.1 Creating a unique list of words

20.1.2 Cleaning the text

20.1.3 Using a set

20.1.4 Filtering the words

20.1.5 Titlecasing the words

20.1.6 Sampling and making a password

20.2 l33t-ify

20.2.1 Putting it all together

20.3 Solution

20.4 Discussion

20.4.1 Cleaning the text

20.4.2 A king’s ransom

20.4.3 How to l33t()

20.4.4 Processing the files

20.4.5 Sampling and creating the passwords

20.5 Review

20.6 Going Further

21 Tic-Tac-Toe: Exploring state

21.1 Writing

21.1.1 Validating user input

21.1.2 Altering the board

21.1.3 Printing the board

21.1.4 Determining a winner

21.2 Solution

21.2.1 Validating the arguments and mutating the board

21.2.2 Formatting the board

21.2.3 Finding the winner

21.3 Review

21.4 Going further

22 Tic-Tac-Toe Redux: An interactive version with type hints

22.1 Writing

22.1.1 Tuple talk

22.1.2 Named Tuples

22.1.3 Adding type hints

22.1.4 Type verification with mypy

22.1.5 Updating immutable structures

22.1.6 Adding type hints to function definitions

22.2 Interactive solution

22.3 A version using TypedDict

22.3.1 Thinking about state

22.4 Review

22.5 Going further

23 Epilogue


Appendix A: Using argparse

A.1 Types of arguments

A.2 Starting off with

A.3 Using argparse

A.3.1 Creating the parser

A.3.2 A positional parameter

A.3.3 An optional string parameter

A.3.4 An optional numeric parameter

A.3.5 An optional file parameter

A.3.6 A flag

A.3.7 Returning from get_args

A.4 Examples using argparse

A.4.1 A single, positional argument

A.4.2 Two different positional arguments

A.4.3 Restricting values using choices

A.4.4 Two of the same positional arguments

A.4.5 One or more of the same positional arguments

A.4.6 File arguments

A.4.7 Manually checking arguments

A.4.8 Automatic help

A.5 Summary

What's inside

Write command-line Python programs Manipulate Python data structures Use and control randomness Write and run tests for programs and functions Download testing suites for each project

About the reader

For readers with beginner programming skills.

About the author

Ken Youens-Clark is a Senior Scientific Programmer at the University of Arizona. He has an MS in Biosystems Engineering and has been programming for over 20 years.


Budite prvi koji će ostaviti komentar.

Ostavite komentar Ostavite komentar



Naučite Python 3, brzi kurs programiranja

Naučite Python 3, brzi kurs programiranja

Modern Python Cookbook

Modern Python Cookbook

Veze, linkovi
Linkedin Twitter Facebook
© Sva prava pridržana, Kompjuter biblioteka, Beograd, Obalskih radnika 4a, Telefon: +381 11 252 0 272