Learn Python Programming Language
Categories: Python
Learning Python Programming Language
As mentioned before, English language keywords make up most of the programming in Python. If you master them, you have mastered Python for the most part. This will take some practice, and you need to know the basic concepts before you start off. So let’s begin by looking at them:
Properties
Python is implicitly and dynamically typed, so you do not have to declare variables. The types are enforced, and the variables are also case sensitive, so var and VAR are treated as two separate variables. If you want to know how any object work, you just need to type the following:
help(object)
you can also use the dir(object) command to find out all the methods of a particular option, and you can use object.__doc__ to find out its document string.
Python does not have a mandatory character to terminate statements. Any blocks are specified using indentation, so you indent to start a block and de-dent to the end one. Statements expecting an indentation level end with a colon. If you want to add comments, use the # sign for each line. Multi-line strings need to be used for multi-line comments. Values are assigned using the “=” sign, and equality testing is done with two of them “==”. You can decrement or increment values with the operators += or -= with the amount on the right-hand side. This can work on strings and other data types. You can also use multiple variables on one line, like so:
Data types
Let’s move ahead to data types. The data structures in Python are dictionaries, tuples and lists. Sets can be found in the sets library that is available in all versions of Python from 2.5 onwards. Lists are similar to one-dimensional arrays, although you can also have lists of other lists. Dictionaries are essentially associative arrays or hash tables. Tuples are one-dimensional arrays. Now, Python arrays can be of any type, and the types are always zero. Negative numbers start from the end to the beginning, and -1 is the last item. Variables can also point to functions. Here is an example of the usage:
You can use the colon to access array ranges. If you leave the start index empty, the interpreter assumes the first item, so the end index assumes the last item. Negative indexes count from the last item, so -1 is seen as the last item. Here is an example:
Adding the third parameter will see the Python step in the N item increments instead of one in the last line. For instance, in the above sample code, the first item is returned and then the third, so items zero and two in zero-indexing.
Strings
Let’s move on to strings. Python strings can either use single or double quotation marks, and you can use quotation marks of one kind in a string using another kind, so the following is valid:
“This is a ‘valid’ string.”
Multi-strings are enclosed in single or triple double-quotes. Python can support Unicode right from the start, using the following syntax:
u”This is Unicode.”
To fill strings with values, you can use the modulo (%) operator and then a tuple. Each % gets replaced with a tuple item from left to right, and you can use dictionary substitutions as well.
Flow control statements
Python’s flow control statements are ‘while’, ‘for’ and ‘if’. For a switch, you need to use ‘if’. For enumerating through list members, use ‘for’. For obtaining a number list, use range (number).
Functions
The ‘def’ keyword is used to declare functions. Optional arguments can be set in the function declaration after mandatory arguments by assigning them default values. In the case of named arguments, the argument name is assigned a value. Functions can return a tuple, and you can effectively return several values using tuple unpacking. Parameters are passed through reference, but tuples, ints, strings, and other immutable types are unchangeable because only the item’s memory location is passed. Binding another object to the variable removed the older one and replaced immutable types.