Python Syntax Basics: Input, Operators, and Conditions



Partner With Us

Python Learning Notes

Python Syntax Basics: Input, Operators, and Conditions

A beginner-friendly Python note on reading input, converting values, using operators, and writing clear if statements.

Share this article

Copy the link, open your phone share sheet, scan the QR code for WeChat, or send by email.




Email

Python Learning Notes

In the previous note, we looked at print(), comments, variables, and basic types. The next step is learning how a small program receives information, performs a calculation, and makes a decision.

That pattern appears everywhere in beginner Python:

  • read input
  • convert values when needed
  • compute something
  • use a condition
  • print the result

Once students understand this flow, many simple CCC Junior style problems become much less mysterious.

Start with Input

Python uses input() to read text from the user.

name = input("Enter your name: ")
print("Hello", name)

The important detail is that input() always gives us a string.

Even if the user types 12, Python receives it as text:

age = input("Enter your age: ")
print(type(age))

For arithmetic, we usually need to convert the value first.

age = int(input("Enter your age: "))
next_year = age + 1
print(next_year)

This is one of the most common early Python mistakes: trying to do math with input strings.

Convert Before Doing Math

Use int() when the input should be a whole number.

first = int(input())
second = int(input())
print(first + second)

Use float() when the input may contain decimals.

price = float(input())
tax = price * 0.13
print(tax)

A good beginner habit is to ask:

  • Is this value text?
  • Is this value a whole number?
  • Is this value a decimal number?

Choosing the right conversion makes the program easier to reason about.

Arithmetic Operators

Python has the arithmetic operators students expect:

print(5 + 2)   # addition
print(5 - 2)   # subtraction
print(5 * 2)   # multiplication
print(5 / 2)   # division
print(5 // 2)  # floor division
print(5 % 2)   # remainder
print(5 ** 2)  # exponent

Two operators are especially useful in programming problems:

  • // gives the whole-number quotient
  • % gives the remainder

For example, n % 2 == 0 is a common way to check whether n is even.

n = int(input())
if n % 2 == 0:
    print("even")
else:
    print("odd")

Assignment Operators

Programs often update a running value.

score = 10
score = score + 5
print(score)

Python also lets us write this more compactly:

score = 10
score += 5
print(score)

The two versions mean the same thing. In beginner code, the longer version is often easier to understand at first. The shorter version becomes natural with practice.

Comparison and Boolean Values

Conditions use comparison operators:

print(5 > 3)
print(5 == 3)
print(5 != 3)

These expressions produce Boolean values:

  • True
  • False

In Python, equality uses ==, not =.

x = 5      # assignment
print(x == 5)  # comparison

This distinction matters. A single = stores a value. A double == asks whether two values are equal.

Writing an if Statement

An if statement lets a program choose between paths.

temperature = int(input())

if temperature >= 30:
    print("hot")
else:
    print("not hot")

Python uses indentation to show which lines belong inside the if or else block. Indentation is not decoration. It is part of the program structure.

A CCC-Style Example

Suppose a roller coaster train has C cars, and each car holds P people. You are the N-th person in line. Will you get on the next ride?

The program can follow the same pattern:

  1. Read input.
  2. Compute the total capacity.
  3. Compare capacity with N.
  4. Print the answer.
N = int(input())
C = int(input())
P = int(input())

capacity = C * P

if capacity >= N:
    print("yes")
else:
    print("no")

This is a small program, but it contains several important habits: clear variable names, conversion before math, a meaningful intermediate value, and a simple condition.

Common Mistakes

  • Forgetting that input() returns a string

If the program needs math, convert with int() or float().

  • Using = when you mean ==

Use = to assign a value. Use == to compare values.

  • Ignoring indentation

Python uses indentation to decide what belongs inside a block.

  • Trying to write everything in one line

Clear beginner programs usually use small steps: read, compute, compare, print.

  • Not testing boundary cases

If the question asks whether capacity is at least N, test the case where capacity equals N.

Practice Prompt

Write a program for a donut shop.

The shop starts with D donuts. Then there are E events. Each event is either + followed by a quantity baked, or - followed by a quantity sold.

Print the number of donuts left at the end.

Example input:

10
3
+
5
-
4
+
2

Example output:

13

Key Takeaway

Many beginner Python programs follow a simple structure: read input, convert values, compute, compare, and print. Once students get comfortable with this pattern, they can solve many small programming problems with much more confidence.

Questions or feedback?

Have a question about this article or want to suggest an improvement? Send us a private message.

Send Feedback

Back to Resources



滚动至顶部