Unit 1 - Basic Python Programming and Simple Data Types

 CBSE Revision Notes

Class-11 Computer Science (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)

Basic Python Programming and Simple Data Types

Familiarization with the basics of Python programming

Guido van Rossum created the Python programming language in the late 1980s. In contrast to other popular languages such as C, C++, Java, and C#, Python strives to provide a simple but powerful syntax. Python is used for software development at companies and organizations such as Google, Yahoo and NASA.

Simple Hello program: Python programs must be written with a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program. When we want to write a program, we use a text editor to write the Python instructions into a file, which is called a script. By convention, Python scripts have names that end with .py. For example we will name our first program as “hello.py”.

>>print(“This is my first program”)

We will consider two ways in which we can run “hello.py”:

  1. Enter the program directly into IDLE’s interactive shell
  2. Enter the program into IDLE’s editor, save it, and run it.

IDLE’s interactive shell:

IDLE is a simple Python integrated development environment available for Windows, Linux, and Mac OS X. To open IDLE interactive shell “Goto start menu on windows -> open IDLE”. You may type the above one line Python program directly into IDLE and press enter to execute the program. Since it does not provide a way to save the code you enter, the interactive shell is not the best tool for writing larger programs. The IDLE interactive shell is useful for experimenting with small snippets of Python code.

IDLE’s editor: IDLE has a built in editor. From the IDLE menu, select New Window, Type the text print(“This is my first program”)  into the editor. You can save your program using the Save option in the File menu. Save the code to a file named “hello.py”. The extension .py is the extension used for Python source code. We can run the program from within the IDLE editor by pressing the F5 function key or from the editor’s Run menu: Run -> Run Module. The output appears in the IDLE interactive shell window.

Print(“This is my  first program”)

This is a Python statement. A statement is a command that the interpreter executes. This statement prints the message “This is my first program” on the screen. A statement is the fundamental unit of execution in a Python program. Statements may be grouped into larger chunks called blocks, and blocks can make up more complex statements. 

The statement print(“This is my first program”) makes use of a built in function named “print”. Python has a variety of different kinds of statements that may be used to build programs.

**We generally write a computer program using a high-level language. A high-level language is one which is understandable by us humans. It contains words and phrases from the English (or other) language. But a computer does not understand high-level language. It only understands program written in 0’s and 1’s  in binary, called the machine code. A program written in high-level language is called a source code. We need to convert the source code into machine code and this is accomplished by compilers and interpreters. Hence, a compiler or an interpreter is a program that converts program written in high-level language into machine code understood by the computer.
**Interpreter: Interpreter executes one statement at a time.
**Compiler: Compiler scans the entire program and translates it as a whole into machine code.

Simple Data-Types

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers.

Some basic data types that python support are integer, float, string.

Integer (int): Int, or integer, is a whole number, positive or negative, without decimals, of unlimited lengthThe number four (4) is an example of a numeric value. In mathematics, 4 is an integer value. Integers are whole numbers, which means they have no fractional parts, and they can be positive, negative, or zero. Examples of integers include 4, −19, 0, and −1005. In contrast, 4.5 is not an integer, since it is not a whole number. Python supports a number of numeric and non-numeric values. In particular, Python programs can use integer values. Let’s take an example integer.py








In above program there are three variable holding integer values in x,y and z. Then there are three print statements which will display the three integer variables and last print statement will display addition of all the 3 numbers that are in the variables.

Float: Many computational tasks require numbers that have fractional parts. For example, to compute the area of a circle given the circle’s radius, the value pi, or approximately 3.14159 is used. Python supports such non integer numbers, and they are called floating point numbers. The name implies that during mathematical calculations the decimal point can move or “float” to various positions within the number to maintain the proper number of significant digits. The Python name for the floating-point type is float. Let’s take an example of float in this program we will calculate area of circle




Print(‘Area of circle is ‘, Area)

In the above example we are taking 3 float variables PI Radius and area. In a statement Area=PI*Radius*radius we are calculating the area of circle and putting the value in variable area. Then in print statement we are printing the area of circle with a message.

String: String literals in python are surrounded by either single quotation marks, or double quotation marks. ‘hello’ is the same as “hello”. String variable holds the text.
Let’s take an example in this example we will see how to print string variables and how to take input from the user.

print("Enter text")

In above program there are 2 variables x and y. x is holding the string “hello” and y will take text from the keyboard at run time and next line will print both the strings.