Skip to content
← Computer Science

Computer Science · 1st Puc Optional

Ch 5Getting Started with Python — 1st PUC Computer Science, concept-first.

A computer can only act on instructions it is given, so before a computer solves a problem for us, we must spell out the solution as a precise sequence of instructions.

49

Q&A

21

Concepts

Not available

Exam weightage

Start learning — read this chapter →

Key concepts

Hover a concept to preview it and jump to its most relevant Q&A.

Variable Naming Convention

Imagine you walk into a messy kitchen. There are three identical unlabeled jars on the counter. One contains salt, one sugar, one baking soda. You need salt for your recipe.

Start with this concept →

Chapter contents

The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.

5.1

Introduction to Python

A computer can only act on instructions it is given, so before a computer solves a problem for us, we must spell out the solution as a precise sequence of instructions.

5.1.1

Features of Python

Python's popularity as a first programming language comes from a specific set of features, each of which shapes how we write and run programs in it.

5.1.2

Working with Python

To write and execute a Python program, one thing is essential: a Python interpreter. There are two ways to get one —

5.1.3

Execution Modes

The Python interpreter can be used in two ways:

5.2

Python Keywords

Some words in Python are not available for us to use as names — the language has already claimed them.

5.3

Identifiers

In programming languages, identifiers are the names used to identify a variable, a function, or any other entity in a program. Choosing them is not free-form — Python lays down precise rules.

5.4

Variables

2 Q

A variable in a program is uniquely identified by a name — an identifier. In Python, a variable refers to an object: an item or element stored in the memory.

5.5

Comments

2 Q

Comments are remarks or notes added inside source code. The interpreter does not execute comments — they exist purely for the humans reading the program.

5.6

Everything is an Object

Python treats every value or data item as an object — whether it is numeric, a string, or of any other type (the types themselves are discussed in the next section).

5.7

Data Types

Every value in Python belongs to a specific data type. A data type does two jobs at once: it identifies what kind of data values a variable can hold, and it determines which operations can be performe…

5.7.1

Number

The Number data type stores numerical values only. It is further classified into three different types — int, float and complex.

5.7.2

Sequence

2 Q

A Python sequence is an ordered collection of items, where each item is indexed by an integer — the position of every element is fixed and numbered.

5.7.3

Set

A set is an unordered collection of items, separated by commas and enclosed in curly brackets . At first glance a set looks similar to a list — both hold multiple comma-separated items — but two pr…

5.7.4

None

None is a special data type with a single value. It is used to signify the absence of value in a situation — a variable that exists but deliberately holds "nothing yet" can be given the value None.

5.7.5

Mapping

Mapping is an unordered data type in Python. Where a sequence looks items up by their integer position, a mapping looks values up by a key.

5.7.6

Mutable and Immutable Data Types

Programs often need to change or update the values of certain variables as they run. For some data types Python allows this freely — but for others, once a variable of that type has been created and a…

5.7.7

Deciding Usage of Python Data Types

Python offers four collection types — list, tuple, set, dictionary — and each has a situation in which it is the preferred choice.

5.8

Operators

An operator is a symbol that tells Python to carry out a specific mathematical or logical operation on one or more values. The values an operator acts upon are called operands.

5.8.1

Arithmetic Operators

Arithmetic operators let a Python program do calculation. Python supports the four basic arithmetic operations — addition, subtraction, multiplication and division — and adds three more: modulus (rema…

5.8.2

Relational Operators

A relational operator compares the values of the operands on either side of it and works out the relationship between them.

5.8.3

Assignment Operators

An assignment operator assigns a value to — or changes the value of — the variable on its left-hand side.

5.8.4

Logical Operators

Python supports three logical operators: and, or and not. They must be written in lower case only — AND or Or will not work, because Python is case-sensitive and its keywords are fixed.

5.8.5

Identity Operators

Identity operators answer a different question from the equality operator ==. Instead of asking "do these two values look the same?", they ask "is the value of a variable of a certain type?" and, more…

5.8.6

Membership Operators

Membership operators are used to check whether a value is a member of a given sequence or not. Instead of comparing two values, they search: "does this item occur anywhere inside that collection?" The…

5.9

Expressions

An expression is a combination of constants, variables and operators. The defining property of an expression is that it always evaluates to a value — Python works it out and produces a single result.

5.9.1

Precedence of Operators

4 Q

When an expression contains different kinds of operators, Python needs a rule to decide which operator to apply first.

5.10

Statement

In Python, a statement is a unit of code that the Python interpreter can execute. Where an expression evaluates to a value, a statement is an instruction — something the interpreter carries out.

5.11

Input and Output

3 Q

A program often needs to interact with its user — to collect some input data from the end user, process it, and present the desired output.

5.12

Type Conversion

Sometimes the data type a value arrives in is not the type our calculation needs. Type conversion is the changing of a variable's data type from one type to another — and a small program shows exactly…

5.12.1

Explicit Conversion

5 Q

Explicit conversion, also called type casting, happens when data type conversion takes place because the programmer forced it in the program. The general form of an explicit data type conversion is:

5.12.2

Implicit Conversion

Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python, without any instruction from the programmer.

5.13

Debugging

A programmer can make mistakes while writing a program — and because of those mistakes the program may fail to execute at all, or may run but produce wrong output.

5.13.1

Syntax Errors

Like every programming language, Python has its own rules that determine its syntax — the grammar its statements must follow.

5.13.2

Logical Errors

A logical error is a bug in the program that causes it to behave incorrectly — the program runs, but does the wrong thing.

5.13.3

Runtime Error

A runtime error causes abnormal termination of the program while it is executing. It arises when a statement is syntactically correct, but the interpreter still cannot execute it.

Summary

- Python is an open-source, high-level, interpreter-based language usable for a multitude of scientific and non-scientific computing purposes.

Exercises

+Show 21 questions21 questions
  1. Q1Which of the following identifier names are invalid and why? i. Serial_no. ii. 1st_Room iii. Hundred\$ iv. Total Marks v. Total_Marks vi. to…Free
  2. Q2Write the corresponding Python assignment statements: a) Assign 10 to variable length and 20 to variable breadth. b) Assign the average of v…Free
  3. Q3Write logical expressions corresponding to the following statements in Python and evaluate the expressions (assuming variables num1, num2, n…Free
  4. Q4Add a pair of parentheses to each expression so that it evaluates to True. a) 0 == 1 == 2 b) 2 + 3 == 4 + 5 == 7 c) 1 < -1 == 3 > 4Preview
  5. Q5Write the output of the following: a) num1 = 4 num2 = num1 + 1 num1 = 2 print (num1, num2) b) num1, num2 = 2, 6 num1, num2 = num2, num1 + 2…Preview
  6. Q6Which data type will be used to represent the following data values and why? a) Number of months in a year b) Resident of Delhi or not c) Mo…Preview
  7. Q7Give the output of the following when num1 = 4, num2 = 3, num3 = 2 a) num1 += num2 + num3 print (num1) b) num1 = num1 ** (num2 + num3) print…Preview
  8. Q8Categorise the following as syntax error, logical error or runtime error: a) 25 / 0 b) num1 = 25; num2 = 0; num1/num2Preview
  9. Q9A dartboard of radius 10 units and the wall it is hanging on are represented using a two-dimensional coordinate system, with the board’s cen…Preview
  10. Q10Write a Python program to convert temperature in degree Celsius to degree Fahrenheit. If water boils at 100 degree C and freezes as 0 degree…Preview
  11. Q11Write a Python program to calculate the amount payable if money has been lent on simple interest. Principal or money lent = P, Rate of inter…Preview
  12. Q12Write a program to calculate in how many days a work will be completed by three persons A, B and C together. A, B, C take x days, y days and…Preview
  13. Q13Write a program to enter two integers and perform all arithmetic operations on them.Preview
  14. Q14Write a program to swap two numbers using a third variable.Preview
  15. Q15Write a program to swap two numbers without using a third variable.Preview
  16. Q16Write a program to repeat the string ‘‘GOOD MORNING” n times. Here ‘n’ is an integer entered by the user.Preview
  17. Q17Write a program to find average of three numbers.Preview
  18. Q18The volume of a sphere with radius r is 4/3πr³. Write a Python program to find the volume of spheres with radius 7cm, 12cm, 16cm, respective…Preview
  19. Q19Write a program that asks the user to enter their name and age. Print a message addressed to the user that tells the user the year in which…Preview
  20. Q20The formula E = mc² states that the equivalent energy (E) can be calculated as the mass (m) multiplied by the speed of light (c = about 3×10…Preview
  21. Q21Presume that a ladder is put upright against a wall. Let variables length and angle store the length of the ladder and the angle that it for…Preview

More questions