I would like to summarize all the concepts I have learned in a set of articles. Such concepts are really simple, but help one in getting started with this programming language.
A note: In this small tutorial, I am using Python 2.7, since Python 3 is not backward-compatible with Python 2.
Readable code? Commented code
Comments in Python are inserted as follows:
# this is a comment a = b + c # this is a comment too
Indenting code is the only option!
Many programming languages (e.g., Java, C++) use curly braces to delimit blocks of code. Python replaces curly braces with indentation.
for i in [1, 2, 3]: print i for j in [1, 2, 3]: print j print i + j
Notice, moreover, that semicolon are not needed in this language, so, do not use them.
Adding functionalities via modules
A module in Python is a third-party feature collector that, if included in your code, allows you to use such features.
Importing a module:
Importing a module:
import re as reg_expr my_regular_expression = reg_expr.compile("[0-9]", reg_expr.I)
Importing only a set of features from a module:
# module: collections # functions: defaultdict, Counter from collections import defaultdict, Counter lookup = defaultdict(int) my_counter = Counter()
Arithmetic: dividing double values made easy
Python executes integer divisions by default, so that, for instance, when we execute $5 / 2$, this equals $2$. Since usually we do not want to obtain this, there is a tweak that allows us to do proper divisions resulting in non-integer numbers:
From now on, $5/2 = 2.5$.
If you want to perform integer division, then, you will write:
from __future__ import division
If you want to perform integer division, then, you will write:
5 // 2
Python functions
A function, as usual, is a portion of code that takes zero or more inputs and returns an output. In Python functions are defined as follows:
def double(x): """ this is the description of the function""" return x * 2
Functions as parameters
As in JavaScript, it is possible to pass functions as parameters to other functions:def apply_to_one(myFunction): return myFunction(1) # double is a function my_double = double x = apply_to_one(my_double)
Lambda functions
Lambda functions are short anonymous functions, declared in Python as follows:
y = apply_to_one(lambda x: x + 4)
Here, I am passing as a function that requires to add $4$ to any provided input. Then, I am passing such function as a parameter to a function that adds $1$ to the input. As a result, the function apply_to_one calls the lambda function passing the value $x = 1$, which results in $x = 1$ plus $x = x + 4$, which in turn returns $x = 5$.
Default arguments for functions
Default arguments need to be specified when you want a value other than the default.
def my_print(message="my default message"): print message my_print("hello") #prints "hello" my_print() #prints "my default message"
Default arguments can be specified by name, if needed:
def subtract(a=0,b=0): return a - b subtract(0,5) #returns -5 subtract(b=5) # returns -5
Strings
Strings, as in JavaScript,can be delimited by single or double quotation marks.my_string = "hello" your_string = 'hello to you'
Exceptions
Why try-catching exceptions, if you can try-except them? Python does it! Forget about the catch clause. You can handle an exception as follows:try: print 0 / 0 except ZeroDivisionError: print "cannot divide by zero"
Control flow
If
Obviously, Python allows to alter the execution flow by performing actions conditionally using an if:
if 1 > 2: print "1 is smaller than 2" elif 1 > 3: print "1 is smaller than 3" else: print "all other conditions failed."
Python has also the ternary if-then-else line:
parity = "even" if z % 2 == 0 else "odd"
While
This is the construct for the while loop:x = 0 while x < 10: x += 1
For
This is the construct for the for loop:for x in range(10) print x
If you need more complex logic, you can use continue and break:
for x in range(10): if x == 3: continue if x == 5: break print x
Boolean values
Booleans in Python work as in other languages, except they are capitalized:- True is the true value
- False is the false value
- None indicates a nonexistent value (similar to other languages' NULL value)
The following values are treated as false: False, None, [] (i.e., an empty list), {} (i.e., an empty dictionary), "", set(), 0, 0.0
Anything else gets treated as True.
Python has:
- an all function, which takes a list and returns True precisely when every element is truthy
- an any function, which returns True when at least one element is truthy
Wrap-up
Well, that's all for basic principles. They are kind of common with other languages.
A little thing that I did not cover is how Python handles data types. Here is a nice explanation about why Python is both a dynamic language and also a strongly typed language. To summarize the whole thing:
- Strong typing means that the type of a value cannot change. For instance, a variable that is born as string remains a string, cannot be converted to numbers. Python is strongly typed: variable types stay as they are.
- Dynamic typing means that runtime objects have a type, as opposed to static typing where variables have a type. Python is a dynamic language.
Nessun commento:
Posta un commento