Recommended resources
The freely available resources this lesson will direct you to.
Hello! Last time, you created and ran a Python program. It printed a fixed sentence. Today, you’ll make a program respond to what someone types: store input, transform text, calculate with numbers, and display the result. These are small building blocks, but you will use them repeatedly when preparing questions and processing data for AI applications.
Keep using whichever workspace you set up—your Python file on Windows or a code cell in the browser. Allow about 40 minutes to read, watch, and run the examples.
Give values names and recognize their types
A variable gives a value a name so you can use it later. The equals sign in document_count = 4 means assign the value on the right to the name on the left; it is not asking whether the two sides are equal. You can assign a new value to the same name later.
Watch Python Variables from Programming with Mosh for the variable concept, common value types, and readable naming conventions.
Python Variables - Python Tutorial for Beginners with Examples | Mosh
Watch Python Variables from Programming with Mosh to see how names hold values and how to recognize the types you will use in this lesson.
Watch variables and types for assignment, integers, floats, Booleans, and strings. Then watch clear variable names for descriptive names and underscores between words. You can stop when the discussion turns to spacing around the assignment operator.
Here are four values you might store while preparing a document-processing task:
topic = "retrieval"
document_count = 4
minutes_per_document = 2.5
is_ready = True
topic is a string (str): text enclosed in quotes. document_count is an integer (int): a whole number. minutes_per_document is a float (float): a number with a decimal point. is_ready is a Boolean (bool), which is either True or False; its capitalization matters. We will use Boolean values to direct program behavior in the next lesson.
A number inside quotes is text, not a number for arithmetic: "4" is a string, while 4 is an integer. If you are unsure what a value currently is, type() can show you:
print(type(topic)) # <class 'str'>
print(type(document_count)) # <class 'int'>
Choose names that describe the value’s purpose. minutes_per_document is easier to work with than x, particularly once a program has several inputs.
Build expressions from values
An expression is code that evaluates to a value. In document_count * minutes_per_document, Python looks up both variables and multiplies their values. Assignment can save the result:
total_minutes = document_count * minutes_per_document
print(total_minutes) # 10.0
Read the indicated parts of Chapter 1 of Automate the Boring Stuff with Python for arithmetic expressions and the way operators behave with strings. The chapter demonstrates an interactive shell; you can enter its examples in your existing file or notebook cell instead.
Chapter 1 - Python Basics, Automate the Boring Stuff with Python, 3rd Ed
Read the expression and string-operator passages in Python Basics from Automate the Boring Stuff with Python. They explain why an operator such as + can produce different results with different types.
Under “Entering Expressions into the Interactive Shell,” start at the paragraph beginning “In Python, 2 + 2 is called an expression.” Read expressions and precedence, through the paragraph immediately after Table 1-1. Focus on how an expression becomes one value and how parentheses affect calculation. Then, under “String Concatenation and Replication,” start at “The meaning of an operator may change.” Read string operators, through the explanation of repetition. Focus on why adding two strings works but adding a string and an integer does not.
For numbers, + adds and * multiplies. For strings, + joins text and multiplying a string by an integer repeats it:
print(4 + 2) # 6
print("document" + " search") # document search
print("ha" * 3) # hahaha
Python does not silently choose between these meanings. "4" + 2 produces a TypeError because one value is text and the other is an integer. You will soon convert input deliberately when you need arithmetic.
Parentheses make numeric calculations easier to read and can change their result: 2 + 3 * 4 is 14, whereas (2 + 3) * 4 is 20. You need not memorize every operator today; the important habit is to identify the types of the values and the value the expression will produce.
Transform strings rather than merely displaying them
Suppose someone supplies a topic with inconsistent capitalization and extra spaces. Python strings have operations called methods that produce transformed text. For example, .strip() removes spaces at the ends of a string, while .lower() makes letters lowercase:
raw_topic = " Vector Databases "
clean_topic = raw_topic.strip().lower()
print(clean_topic) # vector databases
The period joins the string value to the method you want to call. The parentheses call that method. .strip() leaves the space between vector and databases alone. Because the result is assigned to clean_topic, you can retain the original input in raw_topic if you need it.
You can now assemble a sentence with +:
question = "Find documents about " + clean_topic + "."
print(question) # Find documents about vector databases.
An f-string offers a readable alternative. Put f before the opening quote and a variable name inside braces where its value should appear:
question = f"Find documents about {clean_topic}."
print(question) # Find documents about vector databases.
print(len(question)) # 38
len() returns the number of characters in this string, including its spaces and final period. A character count is useful for checking text length, though it is not the same thing as a model’s token count, which you will study later.
Notice that print() displays results; it does not store a transformed string for subsequent work. Assign the result to a variable, as with question, when another line of code needs it.
Turn typed input into usable values
The input() function pauses a running program, displays a prompt, and waits for someone to type a response and press Enter. Watch how the response is stored—and why a response that looks numeric still needs conversion before arithmetic.
User input in Python is easy + exercises ⌨️
Watch User input in Python is easy + exercises from Bro Code for the transition from a fixed program to one that accepts text and numbers.
In capturing input, focus on the prompt and the variable that saves the response. In converting input, watch what happens when arithmetic is attempted on the response and how int() or float() changes its type. The video also displays values with f-strings; the {variable} syntax is the one you just used.
input() always returns a string, even when the person enters 50. That makes the distinction between joining text and adding numbers consequential:
entered_text = "50"
print(entered_text + "1") # 501
print(int(entered_text) + 1) # 51
Use int() for whole-number input and float() when a decimal value is appropriate. For example, a rough time estimate might accept both a whole document count and fractional minutes per document:
documents = int(input("How many documents? "))
minutes_each = float(input("Minutes per document? "))
total_minutes = documents * minutes_each
print(f"Estimated minutes: {total_minutes}")
If you enter 4 and 2.5, the result is Estimated minutes: 10.0. Here, each conversion happens before multiplication. You could instead store the raw strings first and convert them on separate lines; that is often easier to inspect while learning.
Conversion has a limit: int("four") and int("2.5") raise a ValueError. Use float("2.5") for decimal text. For this lesson, enter the kind of value the prompt requests. Later, you will learn how programs respond gracefully to unexpected input.
Put the transformations together
Create a new file or code cell and enter this complete program. On Windows, save the file before running it; in a browser notebook, run the cell and enter each response when prompted.
raw_topic = input("Topic to research: ")
topic = raw_topic.strip().lower()
question = f"Find documents about {topic}."
max_chars_text = input("Maximum question length: ")
max_chars = int(max_chars_text)
remaining = max_chars - len(question)
print("Search question:", question)
print(f"Characters remaining: {remaining}")
Try entering Vector Databases as the topic and 50 as the maximum length. The final lines should be:
Search question: Find documents about vector databases.
Characters remaining: 12
Read the program as a sequence of values being transformed. raw_topic holds exactly what was typed; topic holds the cleaned string. The f-string puts that string into a question. max_chars_text starts as text, so int() converts it before the subtraction. Finally, len(question) contributes the question’s character count, 38. In print("Search question:", question), the comma lets print() display two values with a space between them; it is not string concatenation.
This program also exposes its current limits. A maximum shorter than the question will give a negative remaining count, and non-numeric input for the maximum will cause an error. Those are not reasons to abandon the transformations: they are reasons for the conditional logic coming next.
Key takeaways
Variables retain values for later use; types determine which operations make sense. Expressions calculate new values, string methods clean text, and f-strings place values into readable output. Most importantly, typed input begins as a string, so convert it before numeric calculation.
You can now turn a person’s rough topic and length limit into a cleaned question and a calculated result. In the next lesson, you’ll use conditionals and loops to make a program respond differently when input needs attention.