Creating basic Python programs
Conceptually, a program can be devised into
two distinct categories:
Data: the information you want to work with, such as a person’s name and
phone number.
Operations: are the things you want to do with the data, such as printing out
the data or adding two numbers together.
Practically program may also have few
other things as well, like comments, namespaces, or directives, but the data
and operations are the core of the program. We can list commonly used items as.
Comments: Comments provide additional information to humans. Any code that
follows a comment will not be processed. A comment only affects code on one
line unless you use a block comment.
# This is a standard, inline, Python
comment.
z= x + y # this is
a comment
Block, multi-line comments are not
available in Python
Namespaces: Namespaces specifies an easy way to arrange program’s code into
named groups. A namespace is a simple mechanism to control the names in a
program and ensures that names are unique and won't let any conflict. Python keeps
a namespace in the form of a Python dictionary. We can understand namespace as
combination of two words name and space. Name is represents unique identifier
and space represents scope of the variable or object.
Statements: Statements are the commands in a Python program file. A statement can
be considered as one instruction to the computer. Each of these statements will
be made up of one or more keywords or symbols generally known as tokens. There
can be more than one token per statement. Use of a semicolon(;) at the end of
the statement is optional in Python, but generally this is considered wrong practice.
Variable: If the programmer wants to allow changes to a value while the
program is running, they can create a variable. A variable allows for both
reading and writing of data. A variable is mere holder for the value and is
identified by some identifier. The value of variable is supposed to be changed
during the execution of program.
For example
>>> x = 11
>>> y = 20
Constant: The value of constant is fixed and cannot be changed during the
execution of program. The difference between variables and constants, is that
"constant" data is set with an initial value that cannot be changed
while the program is running.
For example
>>> 3+7
10
First Python program
Go to you Python shell and create few basic
programs
1. Print “Hello World”
>>>
print("hello world")
hello world
2. Print the sum of two numbers x=12 and y=15
>>> x=12
>>> y=15
>>> print(x+y)
27
3. Take input from screen for x and y tow integer variables and print their sum
>>>
x=(int)(input())
15
>>>
>>>
y=(int)(input())
27
>>> print(x+y)
42
4. Python code to check which number is greater
>>>a=10
>>>b=7
>>>if a>b:
print('a')
else:
print('b')
>>>a=10
>>>b=7
>>>if a>b:
print('a')
else:
print('b')