Python is a
general-purpose interpreted, interactive, object-oriented, and high-level
programming language. It was created by Guido van Rossum during 1985- 1990.
There are two
major Python versions, Python 2 and Python 3. Python 2 and 3 are quite
different. This tutorial uses Python 3, because it more semantically correct
and supports newer features.
First Python Program
The simplest
command in python is the "print" command. The print command
prints the output.
In Python 2,
the "print" statement is not a function, and therefore it is invoked
without parentheses.
However, in
Python 3, it is a function, and must be invoked with parentheses.
Below is a
simple syntax to print "Hello World!"
print("Hello World!")
Lines and Indentation
Python provides no braces to
indicate blocks of code for class and function definitions or flow control.
Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the
indentation is variable, but all statements within the block must be indented the same amount. For example −
if True:
print
"True"
else:
print
"False"
However, the following block
generates an error −
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Thus, in Python all the
continuous lines indented with same number of spaces would form a block.
Standard Data Types
Python has various standard data types that are used to
define the operations possible on them and the storage method for each of them.
Python has five standard data types −
- Numbers
- String
- List
- Tuple
- Dictionary
Basic Operators
Operators are the constructs which can manipulate the value
of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called
operands and + is called operator.
Types
of Operator
Python language supports the following types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Functions
A function is a block of organized, reusable code that is
used to perform a single, related action. Functions provide better modularity
for your application and a high degree of code reusing.
Defining a Function
- You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
- Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
- Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
- The first statement of a function can be an optional statement - the documentation string of the function or docstring.
- The code block within every function starts with a colon (:) and is indented.
- The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return
[expression]
No comments:
Post a Comment