1. Home
  2. Explore
  3. Computer Science
  4. Computer and Python Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Computer and Python Basics

Speaker 1Speaker 2

Computer and Python Basics

15:38
0:00-15:38
  1. Maria

    Welcome to our first episode on the foundations of programming. We’ll follow the opening lecture of a full-semester introductory computer science course: a little course orientation, a big-picture look at computers and algorithms, and then the first Python building blocks.

  2. Anna

    The course begins with administrative advice that’s really part of learning how to program. Download the lecture slides so you can take notes and run the code along with the instructor. Lectures include questions and hands-on practice breaks, and there are extra exercises in the handouts and other course resources.

  3. Maria

    That emphasis on practice is important. Programming is a skill, like math or reading: watching someone write code doesn’t automatically prepare you to write it yourself. You improve by doing it repeatedly, so the course is designed to give students many chances to try.

  4. Anna

    The lecture describes three main outcomes: understanding computer science concepts, building programming skill, and developing problem-solving skill. Lectures and exams focus on concepts; short programming exercises build coding ability; and problem sets ask you to turn a problem described in ordinary language into a computational solution.

  5. Maria

    That last step is computational thinking. Instead of asking only how to solve a problem by hand or with a mathematical method, you learn to ask how computation could help. The course will use examples of applying a computer to problems students may already know how to solve another way.

  6. Anna

    To do that, the class will learn Python, then ways to organize code so it’s readable and modular rather than one big pile of instructions. It will also introduce foundational algorithms and, later, algorithmic complexity: how to compare approaches and reason about whether a program is efficient in time and memory.

  7. Maria

    Before Python, there’s a useful distinction between two kinds of knowledge. Declarative knowledge states a fact or relationship; imperative knowledge gives a recipe for doing something. Programming is primarily imperative: we give the computer instructions to carry out.

  8. Anna

    For example, saying that the square root of X is a number Y whose square equals X describes a property. It doesn’t tell a computer how to find Y. A recipe does: start with a guess, check whether its square is close enough, and, if it isn’t, use a specified calculation to form a better guess and repeat.

  9. Maria

    The exact stopping condition matters. You might stop when the guess is within some chosen tolerance of the answer; a loose tolerance gives a rough answer, while a tighter one means continuing the process longer. This sequence of steps, including how to repeat and when to stop, is an algorithm.

  10. Anna

    Recipes have the same basic structure. They give steps in sequence, may include decisions such as substituting an ingredient, and may repeat an action until a condition is met. In that sense, a recipe is an everyday example of an algorithm.

  11. Maria

    And a computer executes algorithms; it doesn’t independently figure out what you meant. Computers are good at storing large amounts of data and performing operations quickly, but they follow the instructions they’re given. One of the lecture’s central ideas is that a computer does only what you tell it to do.

  12. Anna

    A brief history helps explain how instructions became programs. A fixed-program device such as a pocket calculator has operations built in, but it doesn’t store a reusable sequence of operations that you can apply later to different inputs. You have to enter the operations again.

  13. Maria

    Stored-program computers, which appeared after the 1940s, could store instructions as data. An interpreter could execute those instructions, including arithmetic, logical tests such as comparing values, and moving data between memory locations.

  14. Anna

    At a very high level, the lecture describes memory for stored values, an arithmetic logic unit—or ALU—for operations and comparisons, and a control unit that directs execution. A program counter identifies the instruction being carried out. The interpreter follows one instruction, then the next, using those parts.

  15. Maria

    Imagine instructions that add the values stored at two memory locations, put the result in another location, do a second addition, compare the two results, and print whether they match. Even a tiny task takes several low-level instructions: retrieve values, calculate, store, compare, and display.

  16. Anna

    That example shows why modern programming languages are convenient. We can express the same kinds of tasks with more helpful building blocks instead of spelling out every low-level memory operation. The lecture also mentions Alan Turing’s result: a very small set of basic operations can, in principle, compute anything computable.

  17. Maria

    The six tape operations described are moving left, moving right, reading a value, writing a value, erasing a value, and doing nothing. The broader takeaway is that anything computable in one programming language can be computed in another, even if the languages use very different primitives and make the task more or less convenient.

  18. Anna

    A comparison with English helps explain how a language builds meaning. In English, characters form words, words form sentences, and sentences can form larger works. In a programming language, primitives such as numbers, character sequences, arithmetic operators, and comparisons combine according to rules.

  19. Maria

    Those rules give us syntax: whether an expression is structured correctly. English has patterns too—‘noun, verb, noun’ can form a sentence, while simply placing ‘cat, dog, boy’ together doesn’t. Python also expects objects and operators to appear in valid arrangements.

  20. Anna

    For instance, a character sequence and a number placed directly beside each other don’t make a meaningful Python expression. Put the multiplication operator between the string 'Hi' and the number 5, though, and Python interprets it as repeating the string five times.

  21. Maria

    Syntax isn’t the whole story. Static semantics concerns whether a syntactically valid expression makes sense under the language’s rules. Multiplying a string by a number has a defined meaning in this example, but adding a string to a number does not, even if the arrangement looks syntactically like object, operator, object.

  22. Anna

    Then there’s the meaning of a program—its semantics. Natural language can be ambiguous: ‘the chicken is ready to eat’ could mean it’s ready for us to eat, or ready to eat something itself. A computer program has one execution meaning according to its instructions, but that meaning may not be what its author intended.

  23. Maria

    That difference explains several kinds of programming trouble. Syntax errors include things such as misspellings or indentation problems; static semantic errors occur when an operation is not allowed. But a program can also run and still do the wrong thing, crash, or fail to stop—because its actual behavior doesn’t match the programmer’s intention.

  24. Anna

    In Python, we’ll write definitions and commands in a file editor or a shell. The shell is especially handy for trying a quick command or checking an expression without writing and running a whole program. The early lecture uses it to introduce Python’s basic objects and operations.

  25. Maria

    At the core of programming, the lecture says, we create objects and manipulate them. Each object has a type, and its type helps determine which operations make sense for it. A number and a sequence of characters may both be objects, but they don’t support all the same operations.

  26. Anna

    For example, an integer such as 30 supports numerical operations, such as addition and subtraction. The string 'Anna' is a sequence of characters, marked by quotation marks. You can ask about its length or characters, but dividing it by another string or raising it to a power doesn’t make sense in the way arithmetic does for numbers.

  27. Maria

    The lecture introduces four scalar types, or basic Python types. Integers, written as int, are whole numbers such as 5 or negative 100. Floats, written as float, represent values such as 3.27 or 2.0; even though 2.0 is mathematically a whole number, Python treats the decimal form as a float.

  28. Anna

    Booleans, written as bool, have just two values: True and False, with capital letters. The fourth type is NoneType, whose one value is None. These are types; there can be many integer and float values, only two Boolean values, and only that one None value.

  29. Maria

    To inspect an object’s type, use the type command with the object inside parentheses. For example, type of 7 is int, while type of 0.0 is float. Capitalization matters in Python: writing true in lowercase doesn’t mean the Boolean value True and leads to a name error.

  30. Anna

    Objects can also be cast to another type. Calling float on the integer 3 produces a new float object, 3.0; it doesn’t change the original integer. Calling int on 5.9 truncates the fractional part and gives 5, rather than rounding it.

  31. Maria

    Rounding is a separate operation. The lecture’s example says round of 5.9 gives the integer value 6. The type of a result can depend on the operation too, so it’s useful to check with type rather than guessing.

  32. Anna

    An expression combines objects and operations to produce a value. The expression 3 plus 2 evaluates to 5, and its type is int. Python evaluates the expression and stores the resulting value; it doesn’t keep the original expression around as the value.

  33. Maria

    Expressions can be nested, too. Parentheses can control the order of calculations, and you can put an expression inside a command such as type. For a command with parentheses, Python evaluates what’s inside first, then uses that result in the surrounding command.

  34. Anna

    For arithmetic, the lecture gives these type rules: addition, subtraction, and multiplication produce an integer when both operands are integers, and a float if either operand is a float. Division with a single slash produces a float, even when the result is a whole number.

  35. Maria

    Two other useful operators are double slash and percent. Five double-slash three gives the integer quotient, 1; five percent three gives the remainder, 2. Two asterisks mean exponentiation, so 2 double-asterisk 3 is 8.

  36. Anna

    The operator precedence described is exponentiation first, then multiplication, division, floor division, and remainder, and finally addition and subtraction. Parentheses let you override that order. When in doubt, the shell is a practical place to check what an expression evaluates to and what type it returns.

  37. Maria

    To use values conveniently, we assign them names called variables. This is especially useful for a value you need in several places, such as an approximation of pi: rather than repeatedly typing a long decimal, you can bind it to a descriptive name and refer to that name.

  38. Anna

    A programming variable is different from the idea of a variable in a mathematical statement. In a math equation, the equals sign can express a relationship to solve or reason about. In Python, an equals sign is an assignment: it binds the name on the left to the value produced on the right.

  39. Maria

    So for an assignment like m equals 10, the name m is bound to 10. For f equals m times 9.98, Python evaluates the right-hand expression using m’s current value, then binds the resulting value to f. The left side must be a variable name, not a number or an expression such as x times y.

  40. Anna

    That’s why x equals 6 is valid, while 6 equals x is not. A name such as xy can be a variable, and typing that name later retrieves its value. Quotation marks would make 'xy' a string instead; the quotes change what the text means to Python.

  41. Maria

    Good variable names make programs easier to understand. Instead of unexplained single letters, choose names that relate to the problem. Comments, which begin with a hash mark, can explain the purpose of a section of code; the lecture recommends explaining what a chunk is intended to accomplish rather than merely translating each line into English.

  42. Anna

    Consider code that calculates a circle’s area and circumference. Descriptive names such as pi, radius, area, and circumference tell the reader what the values represent. A useful comment might explain that the calculations use an approximation for pi, rather than simply restating that one line multiplies pi by the radius twice.

  43. Maria

    Assignments can rebind a variable. In radius equals radius plus 1, Python first looks up the current radius, adds 1, and then binds the name radius to the new value. The equals sign isn’t asserting that both sides are equal; it’s an instruction to calculate on the right and assign on the left.

  44. Anna

    That line-by-line behavior has an important consequence. If area was calculated using radius 2.2 and you later change radius to 3.2, area stays at its earlier value unless you tell Python to calculate area again. The computer doesn’t infer that a dependent value should automatically update.

  45. Maria

    A step-by-step tool such as Python Tutor can make this easier to see. You can execute a program one line at a time and inspect the variables after each line. For example, after setting meters to 100 and calculating feet from meters, changing meters to 200 doesn’t change the feet value that was already calculated.

  46. Anna

    The same idea helps explain how to swap two values. If x is 1 and y is 2, setting y equal to x first destroys y’s old value; then setting x equal to y leaves both names bound to 1. The original 2 is no longer accessible through either of those names.

  47. Maria

    Use a temporary variable to preserve one value. Set temp equal to y, then set y equal to x, and finally set x equal to temp. Now x is 2 and y is 1, because temp held on to the original value of y while the other assignments happened.

  48. Anna

    And if you make a mistake while testing in the shell, that’s okay—you can reassign the variables and try again. The shell is useful for quick experiments, while a step-through tool helps you see how values change as the program runs.

  49. Maria

    Let’s bring the main ideas together. Programs manipulate typed objects, combine them into expressions that evaluate to values, and use variables to access those values by name. Assignment binds or rebinds a name; it is not mathematical equality.

  50. Anna

    Most important, computers follow the instructions they’re given, in order. Learning to program means learning to give those instructions carefully—and practicing enough to turn a problem into a clear sequence of steps. Next, the course will build on these foundations by introducing ways to make decisions in programs.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent