The start of programming fundamentals is with the introduction to Python.It is newly introduced.
Python
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/ and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.
The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.
Python Features
Python's features include:- Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.
- Easy-to-read: Python code is more clearly defined and visible to the eyes.
- Easy-to-maintain: Python's source code is fairly easy-to-maintain.
- A broad standard library: Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.
- Interactive Mode:Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
- Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
- Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.
- Databases: Python provides interfaces to all major commercial databases.
- GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.
- Scalable: Python provides a better structure and support for large programs than shell scripting.
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.
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.if True: print "True" else: print "False"
Multi-Line Statements
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example −total = item_one + \ item_two + \ item_three.
Python Variables
A variable is a location in memory used to store some data (value). They are given unique names to differentiate between different memory locations.Variable assignment
We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable.a = 5
b = 3.2
c = "Hello".
Datatypes in Python
Every value in Python has a datatype. Since everything is an object in Python programming, datatypes are actually classes and variables are instance (object) of these classes.Python Numbers
Integers, floating point numbers and complex numbers falls under Python numbers category.Python List
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type. Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].
>>> a = [1, 2.2, 'python']
>>> type(a)
<class 'list'>
Python Tuple
>>> t = (5,'program', 1+3j)
>>> type(t)
<class 'tuple'>
Python Strings
String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes,'''
or """
.
>>> s = "This is a string"
>>> type(s)
<class 'str'>
>>> s = '''a multiline
... string'''
Python Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.
>>> a = {5,2,3,1,4}
>>> a
{1, 2, 3, 4, 5}
>>> type(a)
<class 'set'>
Python Dictionary
Dictionary is an unordered collection of key-value pairs. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. In Python, dictionaries are defined within braces {} with each item being a pair in the formkey:value
. Key and value can be of any type.
>>> d = {1:'value','key':2}
>>> type(d)
<class 'dict'>
Python Operators
Python Operators
Operators
are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the
operand. For example:
Here is an example.
Here is an example.
Here is an example.
Bitwise operators act
on operands as if they were string of binary digits. It operates bit by
bit, hence the name. For example, 2 is
Let x = 10 (
Here is an example.
Here is an example.
>>> 2+3
5
Here, +
is the operator that performs addition. 2
and 3
are the operands and 5
is the output of the operation. Python has a number of operators which are classified below.Arithmetic operators |
Comparison (Relational) operators |
Logical (Boolean) operators |
Bitwise operators |
Assignment operators |
Special operators |
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | x + y +2 |
- | Subtract right operand from the left or unary minus | x - y -2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus - remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division - division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent - left operand raised to the power of right | x**y (x to the power y) |
x = 15
y = 4
print('x + y = ',x+y)
print('x - y = ',x-y)
print('x * y = ',x*y)
print('x / y = ',x/y)
print('x // y = ',x//y)
print('x ** y = ',x**y)
Outputx + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625
Comparison operators
Comparison operators are used to compare values. It either returnsTrue
or False
according to the condition.Operator | Meaning | Example |
---|---|---|
> | Greater that - True if left operand is greater than the right | x > y |
< | Less that - True if left operand is less than the right | x < y |
== | Equal to - True if both operands are equal | x == y |
!= | Not equal to - True if operands are not equal | x != y |
>= | Greater than or equal to - True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to - True if left operand is less than or equal to the right | x <= y |
x = 10
y = 12
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Outputx > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
Logical operators
Logical operators are theand
, or
, not
operators.Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Outputx and y is False x or y is True not x is FalseHere is the truth table for these operators.
Bitwise operators
10
in binary and 7 is 111
.Let x = 10 (
0000 1010
in binary) and y = 4 (0000 0100
in binary)Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | x& y = 0 (0000 0000 ) |
| | Bitwise OR | x | y = 14 (0000 1110 ) |
~ | Bitwise NOT | ~x = -11 (1111 0101 ) |
^ | Bitwise XOR | x ^ y = 14 (0000 1110 ) |
>> | Bitwise right shift | x>> 2 = 2 (0000 0010 ) |
<< | Bitwise left shift | x<< 2 = 42 (0010 1000 ) |
Assignment operators
Assignment operators are used in Python to assign values to variables.a = 5
is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators in Python like a += 5
that adds to the variable and later assigns the same. It is equivalent to a = a + 5
.Operator | Example | Equivatent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Special operators
Python language offers some special type of operators like the identity operator or the membership operator. They are described below with examples.Identity operators
is
and is not
are the identity operators in Python. They are used to check if two
values (or variables) are located on the same part of the memory. Two
variables that are equal does not imply that they are identical.Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1)
print(x2 is y2)
print(x3 is y3)
OutputFalse True FalseHere, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings). But x3 and y3 are list. They are equal but not identical. Since list are mutable (can be changed), interpreter locates them separately in memory although they are equal.
Membership operators
in
and not in
are the membership operators in Python. They are used to test whether a
value or variable is found in a sequence (string, list, tuple, set and
dictionary). In a dictionary we can only test for presence of key, not
the value.Operator | Meaning | Example |
---|---|---|
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x |
x = 'Hello world'
y = {1:'a',2:'b'}
print('H' in x)
print('hello' not in x)
print(1 in y)
print('a' in y)
OutputTrue True True False
Here,
'H'
is in x but 'hello'
is not present in x (remember, Python is case sensitive). Similary, 1
is key and 'a'
is the value in dictionary y. Hence, 'a' in y
returns False
.
thank you.
ReplyDeletepython3 tutorial
java tutorial