1. RUNNING PYTHON SCRIPTS.
On most of the UNIX systems, you can run Python scripts from the command line in the following manner.
2. RUNNING PYTHON PROGRAMS FROM PYTHON INTERPRETER.
The Python interactive interpreter is very easy to use. You can try your first steps in programming and use any Python command. You just type the command at the Python console, one by one, and the answer is immediate.
Python console can be started by issuing the command:
In this article, all the code starting at the >>> symbol is meant to be given at the Python prompt. It is also important to remember that Python takes tabs very seriously – so if you are receiving any error that mentions tabs, correct the tab spacing.
3. USING ENUMERATE() FUNCTION.
The enumerate() function adds a counter to an iterable object. An iterable is an object that has an __iter__ method which returns an iterator. It can accept sequential indexes starting from zero. And raises an IndexError when the indexes are no longer valid.
A fairly common example of enumerate() function is to loop over a list and keep track of the index. For this, we could use a count variable. But python gives us a nicer syntax for this using the enumerate() function.
4. THE DATA TYPE SET.
The data type “set” is a type of collection. It has been part of Python since version 2.4. A set contains an unordered collection of unique and immutable objects. The set data type is a Python implementation of the sets from mathematics. This explains, why sets unlike lists or tuples can’t have multiple occurrences of the same element.
If you want to create a set, just use the built-in set() function with a sequence or another iterable object.
5. DYNAMIC TYPING.
In Java, C++, and other statically typed languages, you have to specify the data type of the function return value as well as the type of each function argument. On the other hand, Python is a dynamically typed language. In Python, you don’t need to explicitly provide the data types. Based on the value you’ve assigned, Python keeps track of the data type internally. Another good definition of dynamic typing is as follows.
“Names are bound to objects at execution time by means of assignment statements. And it is possible to bind a name to objects of different types during the execution of the program.”
The following example demonstrates how a function can examine its own arguments. And do different things depending on their types.
6. == AND = OPERATORS.
Python uses ‘==’ for comparison and ‘=’ for assignment. Python does not support inline assignment. So there’s no chance of accidentally assigning the value when you actually want to compare it.
7. CONDITIONAL EXPRESSIONS.
Python allows for conditional expressions. So instead of writing an if .. else with just one variable assignment in each branch, follow the below example.
8. CONCATENATING STRINGS.
You can use ‘+’ to concatenate strings in the following manner.
9. THE __INIT__ METHOD.
The __init__ method is invoked soon after the object of a class is instantiated. The method is useful to perform any initialization you plan. The __init__ method is analogous to a constructor in C++, C# or Java.
Output of the above code would look like as given below.
10. MODULES.
To keep your programs manageable as they grow in size, you may want to break them up into several files. Python allows you to put multiple function definitions into a file and use them as a module that can be imported into other scripts and programs. These files must have a .py extension.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# file my_function.py
# Module definition
def minmax(a,b):
if a <= b:
min, max = a, b
else:
min, max = b, a
return min, max
# Module Usage
import my_function
x,y = my_function.minmax(25, 6.3)
|
No comments:
Post a Comment