Programming fundamentals¶
~12 min
In short¶
Programming fundamentals are the small set of ideas that every later CS subject uses: values, types, variables, conditions, loops, functions, and reading errors. This page tells you what to learn first, what to leave for later, and where the Python course on this site covers each idea.
What the subject is about¶
A program is a list of instructions that a computer runs in order. Most of the work in a first-year programming course is about three things. You learn how to describe data (numbers, text, lists of values). You learn how to make decisions and repeat work (conditions and loops). You learn how to split a large task into named pieces (functions). Once you have these, you can read most code written by other people, even in languages you do not know yet.
The subject is not really about one language. Python is a good first language because its syntax is short and its error messages are readable. The ideas carry over to C, Java, JavaScript, and Go.
What to learn first¶
- Running a program, and what
printandinputdo. - Variables, the basic types (integer, float, string, boolean), and what a type error looks like.
- Arithmetic, integer division, and the order of operations.
- Comparisons and
if,elif,else. whileloops andforloops withrange.- Lists and dictionaries, and how to loop over them.
- Writing and calling functions with parameters and return values.
- Scope: where a variable is visible and where it is not.
- Reading a traceback from the bottom line upward.
- Reading and writing small text files, and catching an exception.
The site's Python course covers these in order. Start with ../../python/01-getting-started/index.md, then ../../python/02-decisions-and-loops/index.md, ../../python/03-data/index.md, and ../../python/04-functions-and-structure/index.md.
What you can skip at first¶
- Classes and inheritance. You will need them, but ../../python/07-objects/index.md makes more sense after functions feel easy.
- Recursion beyond the simplest cases.
- Speed tricks for competitive programming. The site has them in ../../python/06-codeforces-patterns/index.md, but they are not needed in your first months.
- Frameworks and web servers. The backend section in ../../python/08-backend/index.md can wait.
- Choosing between many editors and setups. Pick one and move on.
How it connects to the other subjects¶
Programming is the tool you use to test every other subject. When you study data structures and algorithms, you implement them. When you study databases, you send SQL from a program. When you study operating systems or networks, you write small programs that open files or sockets. A weak grip on loops and functions makes all of these slower.
Programming also gives you the vocabulary for the rest of this folder. Terms such as function, variable, and type appear in the glossary.
Free resources¶
- The official Python tutorial (https://docs.python.org/3/tutorial/). Read it as a reference alongside the course. It suits anyone who wants the language's own description of its features.
- CS50x, Harvard (https://cs50.harvard.edu/x/). A full first course in computer science with weekly problem sets. It suits someone who wants a structured class with deadlines and a community.
- The Missing Semester of Your CS Education, MIT (https://missing.csail.mit.edu/). Covers the shell, editors, and version control that programming courses often assume you know. It suits anyone who gets stuck on the computer rather than on the code.
- Teach Yourself Computer Science (https://teachyourselfcs.com/). A reading order, not a course. It suits someone who wants to know what to study after the basics.
Practice¶
- Write a program that asks for a name and a number, and prints the name that many times, one per line.
- Write a function
is_even(n)that returnsTrueorFalse. Then use it in a loop to print only the even numbers from 1 to 30. - Given a list of marks, write code that prints the average, the highest mark, and the count of marks above 50. Do not use any library other than the built-ins.
- Take a traceback from a program you wrote with a bug. Write down, in your own words, the line that caused the error and the type of error. Then fix it.
- Write a program that reads a text file, counts the lines, and handles the case where the file does not exist.
Sources¶
- Python Software Foundation, The Python Tutorial, https://docs.python.org/3/tutorial/
- Harvard University, CS50x, https://cs50.harvard.edu/x/
- MIT, The Missing Semester of Your CS Education, https://missing.csail.mit.edu/
- Teach Yourself Computer Science, https://teachyourselfcs.com/