Concepts and Vocabulary Flashcards
PROGRAMMING LANGUAGES
• What is a programming language?
• What’s unique about Python?
• How does a programming language affect the way we think about solving problems?
A programming language is a set of rules for giving instructions to a computer. It provides the syntax for giving instructions and specifies the ways to store information, and it controls the order in which instructions are executed in a program.
Python is a high-level programming language, which means it takes care of many low-level tasks for you so you can focus on solving problems. For example, when you assign a value to a variable, Python deletes the variable automatically when it’s no longer needed, sparing you from having to manage memory.
Every language has unique features that lead to characteristic programming styles and philosophies. Python focuses on simplicity, readability, and getting the job done.
OPERATING SYSTEMS
• What is an operating system?
• What does an operating system do?
• How does Python interact with the operating system?
The operating system (OS) is the software that controls the computer’s inner workings.
An operating system performs low-level functionality, such as reading from and writing to memory, and interacts with hardware devices, like hard drives, RAM, CPUs, GPUs, dis-plays, batteries, and other external devices. Windows, macOS, and Linux (such as Ubuntu and Fedora) are major operating systems.
Python is a cross-platform programming language. You can write Python code on any OS, and it will run on any other OS.
TERMINAL
• What is a terminal?
• How do you run a Python program from a terminal?
• How do you start a Python session from a terminal?
A terminal is a program that allows you to interact with the OS, and it is often referred to as the console or command line. You use a terminal (rather than going through a GUI) to issue clear, concise, text-based commands to the OS to quickly perform tasks.
You can run a Python program from a terminal using a command like this:
$ python hello_world.py
You can also start a Python session in a terminal. In a Python terminal session, each line of code executes as soon as you enter it:
$ python >>> print("Hello, terminal world!") Hello, terminal world!
TEXT EDITORS
• What is a text editor?
• What is syntax highlighting?
• What are some beginner-friendly text editors?
• What are some more advanced text editors?
A text editor is a program designed for writing and editing code.
Most text editors have features to make writing and editing code easier: syntax highlighting, for example, colors your code so you can quickly recognize different parts of a program - a string might be green and a method might be purple.
VS Code, Sublime Text, and Geany are some commonly used beginner-friendly text editors because of their ease of use and familiar interfaces. They also have powerful tea-tures that help you work more efficiently as you learn.
Emacs and Vim are advanced text editors that were introduced in the 1970s. Their learning curve is steep, but once you learn to use them well, writing and editing code is incredibly efficient. Most Linux systems install Vim, or its predecessor vi, by default.
IDES
• What is an IDE?
• What are some typical features of IDEs?
• What IDEs are best for Python?
An integrated development environment (IDE) is a text editor with powerful project management features.
Typical IDE features include debugging tools, auto-filling for certain code elements, and the ability to catch errors as you’re entering code. For projects that span multiple files, the IDE looks through the files and helps maintain consistency across the project.
IDEs can make code testing easier and identity portions of your code that you could refactor. IDEs help you interact with other project elements, such as HTML and JavaScript in a web application, and help you work with a database.
Popular Python IDEs include PyCharm and VS Code. (VS Code can behave like a simple text editor, but it also has many features commonly associated with IDEs.)
COMMENTS
• What are comments?
• Why are comments useful in programming?
• What kinds of comments should you write?
Comments are lines in a program that the program ignores when it executes. They allow you to add notes about how the program works to help you and other developers understand the code.
Use comments to explain:
• The role of important variables when you introduce them
• How you’ve approached a problem after considering multiple approaches
• What your functions do
• What classes are used for in the program
Writing comments will remind you what your code does when you return to it later. Comments also help teams of programmers collaborate ettectively.
STYLE GUIDES
• What is a style guide?
• What kinds of recommendations are made in style guides?
• Where can you find the Python style guide?
A style guide offers direction on creating consistency in your code, such as how far you indent lines, your maximum line length, or how you break lengthy lines.
A style guide is not a set of rules: if you break the guidelines but follow the syntax rules, your code will still run. When you follow a style guide, your code will be consistent, and it’ll be easier for you and others to focus on what it does rather than what it looks like.
Python’s design ensures that programmers write more readable code with it than with other languages. Be sure to read the Python style guide, PEP 8, for suggestions on how to create clean and consistent code.
PROJECT SPECIFICATIONS
• What is a project specification?
• Why are specifications important?
• Who writes project specifications?
A project specification, or spec, lists requirements for what a program needs to do.
A clear specification is important in projects of all sizes so you have a solid idea of what to aim toward and whether your project is successful. Without a clear spec, you’ll waste time and risk your project’s failure.
A project specification indicates the problems that need solving and how users will interact with the program. It should specify what kinds of inputs your program will deal with, as well as the outputs the program needs to generate.
When learning to program, each exercise you attempt is a mini spec. Well-specified problems are easier to solve than poorly specified exercises. A good programmer looks for a spec when collaborating on an existing project and develops a full spec before committing to a new project.
SYNTAX
• What is syntax?
• What is a syntax error?
• What is a logical error?
The syntax of a programming language is the set of rules for how to write instructions in that language; syntax is like the grammar for that lan-guage. The syntax rules tell you how to store data, respond to specific conditions, and control the flow of execution in the language.
A syntax error occurs when a program doesn’t follow the syntax rules. The language won’t understand how to execute the code, even if only a minor mistake is made, such as a missing parenthesis or comma.
A logical error occurs when your program follows the syntax rules and runs, but doesn’t do what you want it to do. You’ll know you have a logical error when your project generates output but the output doesn’t match the project spec. You’ll need to reevaluate your approach to the problem and then modify that approach.
DEBUGGING
• What is a bug?
• What does debugging mean?
• How do you debug a program?
A bug is a problem in the way a program runs. Debugging is the process of identifying the cause of a bug and modifying code to fix the issue.
One of the first steps in debugging is to understand error messages. They tell you where the interpreter had a problem and what kind of problem occurred.
Logical errors don’t result in error messages, so you need to look at the program execution to resolve them. To debug logical errors, examine the value of variables at different points in the program’s execution. Do this by inserting print() calls into the code at strategic points, logging the values of key variables during execution, or using the debugging tools available in most IDEs. Also, write tests for your code to see which parts are working and which are not.
REFACTORING
• What is refactoring?
• When and why should you refactor code?
• What is the DRY principle?
• How do you refactor code?
Refactoring is the process of rewriting parts of a working program to make it simpler, more efficient, and easier to work with.
Consider refactoring code when you’ve solved the same problem in multiple places. This approach follows the don’t repeat yourself, or DRY, principle. Code repetition presents opportunities for errors, makes programs harder to modify, and makes programs longer and harder to read.
To refactor code, place a repeated block of code into a separate function. Then replace the repeated code with a function call. When you need to improve that code, you only need to change the code in the function for it to work throughout the program. Refactoring can help you find more efficient ways to solve problems, and separating code into different modules makes it easier to work with.
STANDARD LIBRARY
• What is a standard library?
• What is usually included in a standard library?
• What is not included in a standard library?
A standard library is the set of tools included in a language’s standard installation. The library includes core data structures and tools for working with data in your program.
Python’s standard library includes tools to help you:
• Work with a variety of data types and text
• Use mathematical functions
• Work with files and dates and times
• Make graphical user interfaces (GUls)
• Work with networks and multimedia
• Test and debug your code and handle errors
• Distribute your programs
Seldom does a standard library include specialized data analysis tools, game frameworks, web application frame-works, and other application-specific libraries. These are available through external libraries that are updated more often than the language as a whole.
Once you understand a language’s core syntax and rules, become familiar with the language’s standard library tools as these can help you write code efficiently.
THIRD-PARTY LIBRARIES
• What is a third-party library?
• What is a package?
• What is a package manager?
• What kinds of third-party libraries are available for Python?
A third-party library adds tools and functionality that aren’t covered by a language’s standard library.
Third-party libraries, or packages, are installed through an automated package manager, such as pip, for consistent and secure installation. A package manager also helps keep the libraries you’ve installed up-to-date.
More than 400,000 packages are available through the Python package manager. For example, the requests package helps you write programs to access online resources, Pillow helps you work with images, SQLAlchemy makes it easier to work with databases, and NumPy offers tools for working with numerical data.
FRAMEWORKS
• What is a framework?
• What kinds of frameworks are available for Python?
A framework is a larger package that helps solve a particular problem. Some frameworks help you build games, data visualizations, and web applications.
A framework includes tools that handle common tasks within a problem area. For example, a game framework often provides a way to determine when a collision between two game elements has occurred. A web application framework usually provides a way to extract information from a database.
Simple frameworks leave it to you to make many problem-solving decisions; larger frameworks have more default approaches to common situations.
Popular Python web frameworks include Django, a large framework that provides tools to build web applications, and Flask, a bare-bones web application framework that leaves many decisions up to you. Popular game frameworks include Pygame, Kivy, and Pyglet.
ERROR HANDLING
• Why is error handling important?
• How do you handle errors in a program?
• What is an exception?
Error handling refers to writing code that anticipates errors that are likely to occur and then responds to those errors.
To run successfully, a program needs the correct set of inputs and the ability to do its work, and it must return its output appropriately. Errors can happen during any of these stages. Well-written programs have error-handling code that anticipates and deals with errors that might occur at each stage.
Error handling can just be code that checks certain values and conditions before proceeding. If the values and conditions are appropriate, execution continues along one path. If not, it follows a different path.
One common approach is to check whether input is in the correct format. When a user inputs their age, you expect a positive number. You can add code to check for a positive number before continuing and then raise an exception if anything else is entered. An exception is a special error condition that responds to a specific kind of error.