Computer Programming and Python Fundamentals
What this domain covers
PCEP's first domain checks whether you can read and write short Python snippets built from literals, variables, operators and built-in I/O. Questions are usually single-line expressions where precedence, type or a subtle naming rule decides the answer.
Core rules
- Identifiers are case-sensitive, may contain letters, digits and underscores, but cannot begin with a digit. Keywords such as
class,defandNoneare reserved.myVar,_x2and总量are legal;2xandmy-varare not. - Literals include integers (
0,1_000), floats (2.0,1e3), strings, booleans (True,False) andNone. - Operators:
+ - * / // % **./always returns a float;//floors toward negative infinity, so-7 // 2 == -4;%follows the sign of the divisor, so-7 % 2 == 1;**binds tighter than unary minus, so-2 ** 2 == -4. - Precedence roughly:
**, unary,* / // %,+ -, comparisons,not,and,or. Parentheses override it. input()always returns astr; convert withint()orfloat()before arithmetic.print(a, b, sep=",", end="")controls separators and line endings.
Worked example
x = input() and the user types 3; then y = 2; print(x * y) outputs 33, not 6. x is the string "3", and str * int repeats the string. Fix it with x = int(input()).
Traps
=assigns,==compares; using=in anifis aSyntaxError.1 + 2.0yields3.0; mixing int and float promotes to float.bool("")isFalse, butbool("0")isTrue.round(2.5)is2(banker's rounding), andint("2.5")raisesValueError.