Variables, Data Types and Reading Input
Quick answer A quick, exam-shaped recap: what a variable is, the naming rules examiners ask for, the four data types you must name, and why input() always hands you a string.
Python is a high-level programming language — a language written in words close to English, which the computer translates into machine instructions for you. Everything in this chapter follows Python 3. If you meet older code that writes print "Hello" without brackets, that is Python 2 and it will not run in Python 3, so do not copy that style into your answer sheet.
A variable is a named location in memory that stores a value. You create one simply by assigning to it with the assignment operator =. There is no separate declaration line in Python: the moment you assign, the variable exists and its type is decided by the value you gave it.
name = "Ananya"
roll = 21
fee = 1250.50
passed = TrueThe name of a variable is called an identifier, and the rules for a valid identifier are asked for in exactly this form. An identifier may contain letters, digits and the underscore. It must not begin with a digit. It must not contain a space or a special symbol such as @, - or #. It must not be a keyword — a word already reserved by Python, such as if, else, for, while, True or break. Finally, identifiers are case sensitive, so Marks, marks and MARKS are three different variables. A name such as total_fee is valid; total fee, 2ndterm and class are not.
The data type of a value tells Python what kind of data it is and what may be done with it. Four types cover almost every question at this level, and a fifth (the list) has a section of its own.
- int — a whole number, positive, negative or zero:
21,-7,0. - float — a number with a decimal point:
1250.50,3.0. - str — a string, that is text in quotes:
"Ananya",'10'. - bool — a Boolean value, only
TrueorFalse, written with a capital letter.
The built-in function type() reports the type: print(type(fee)) shows that fee is a float. Python is dynamically typed, which means the type follows the value — assigning roll = "twenty one" later would make roll a string.
Output is produced by print(). When you pass several items separated by commas, Python prints them in order with a single space between them, then moves to a new line.
Input is read by input(), and here is the point examiners test most: input() always returns a string, even when the user types digits. To calculate with it you must convert, using int() for whole numbers or float() for decimals. Forgetting the conversion is why 2 and 3 sometimes join into "23" instead of adding to 5.
price = int(input("Enter price in rupees: "))
qty = int(input("Enter quantity: "))
print("Total payable =", price * qty)The arithmetic operators are +, -, *, /, //, % and **. Two of them are examined constantly: / is true division and always gives a float, so 17 / 5 is 3.4, while // is floor division and gives the whole part, so 17 // 5 is 3. The remainder operator % gives 17 % 5 as 2, and it is the standard way to test divisibility: a number n is even when n % 2 == 0. Exponentiation ** gives 2 ** 3 as 8.
Anything after a # on a line is a comment; Python ignores it. Comments do not earn marks by themselves, but one line explaining a tricky step reads well and costs nothing.
- A variable is a named memory location; assignment with = creates it and its type follows the value.
- Identifier rules: letters, digits and underscore only; no leading digit, no spaces, no keywords; case sensitive.
- The four basic types to name in an answer are int, float, str and bool; type() reports the type.
- input() always returns a string — wrap it in int() or float() before doing arithmetic.
- / gives a float (17 / 5 is 3.4), // gives the whole part (17 // 5 is 3), % gives the remainder.
- n % 2 == 0 is the standard test for an even number.
