Python language basics

Python uses the hash mark for a comment, just like R. You can execute Python code in VS 2017 by highlighting the code and simultaneously pressing the Ctrl and Enter keys. Again this is completely the same as you need to do either in RStudio IDE or in R Tools for VS. You can use either single or double apostrophes for delimiting strings. The first command you will learn is the print command. Write and execute the following code in the Visual Studio 2017 Python script window:

# Introducing Python 
# Hash starts a comment 
print("Hello World!") 
# Next command ignored 
# print("Nice typing") 
print('Printing again.') 
print('O"Hara')   # In-line comment 
print("O'Hara") 

You can observe the code you wrote and the results in the interactive window, which, by default, follows the script window, at the bottom left-side of the screen.

Python supports all the basic mathematical and comparison operators, as you would expect. Note that you can combine strings and expressions in a single print statement. The following code introduces them:

1 + 2 
print("The result of 3 + 20 / 4 is:", 3 + 20 / 4) 
10 * 2 - 7 
10 % 4 
print("Is 7 less or equal to 5?", 7 <= 5) 
print("Is 7 greater than 5?", 7 > 5) 
7 is 5 
2 == 3 
2 != 3 
2 ** 3 
2 / 3 
2 // 3   # integer division 
5 // 2 

Here are the results of the last three lines of the previous code:

    >>> 2 / 3
    0.6666666666666666
    >>> 2 // 3
    0
    >>> 5 // 2
    2
  

The next step is to introduce the variables. Note that Python is case sensitive. The following code shows how you can assign values to variables and use them for direct computations and as the arguments of a function. You can also note the case sensitivity error from the following code:

# Integer 
a = 2 
b = 3 
a ** b 
# Case sensitivity 
a + B 
# NameError: name 'B' is not defined 
# Float 
c = 7.0 
d = float(5) 
print(c, d) 
round(4.33777, 3)

You can define strings inside double or single quotes. This enables you to use single quotes inside a double-quoted string, and vice versa. You can use the %? operator for formatting strings to include variables, where the question mark stands for a single letter denoting the data type of the variable, for example s for strings and d for numbers. Here are some examples:

e = "String 1" 
f = "String 2" 
print(e + ", " + f) 
print("Let's concatenate %s and %s in a single string." % (e, f)) 
g = 10 
print("Let's concatenate string %s and number %d." % (e, g)) 

The resulting strings are:

    String 1, String 2
    Let's concatenate String 1 and String 2 in a single string.
    Let's concatenate string String 1 and number 10.
  

Note that because double quotes were used as the string delimiters, it was possible to use a single quote inside a string, in the word Let's.

The str.format() method of the string data type allows you to do variable substitutions in a string, as the following example shows:

four_cb = "String {} {} {} {}" 
print(four_cb.format(1, 2, 3, 4)) 
print(four_cb.format("a", "b", "c", "d")) 

The result of the previous code is:

    String 1 2 3 4
    String a b c d
  

You can also create multi-line strings. Just enclose the strings in a pair of three double quotes. You can also use special characters, such as tab and line feed. Escape them with a single backslash character plus a letter, for example letter t for a tab and letter n for a line feed. The following code shows some examples:

print("""Note three double quotes. 
Allow you to print multiple lines. 
As many as you wish.""") 
a = "I am 5'11" tall" 
b = 'I am 5'11" tall' 
print("	" + a + "
	" + b) 

Here are the results:

    Note three double quotes.
    Allow you to print multiple lines.
    As many as you wish.
          I am 5'11" tall
          I am 5'11" tall
  

You can always get interactive help with the help() command. A Python module is a file with the default extension .py containing Python definitions and statements. You can import a module into your current script with the import command, and then use the functions and variables defined in that module. Besides modules provided with the installation, you can, of course, develop your own modules, distribute them, and reuse the code. The following example shows how you can import the module sys, get help about it, and check the version of the Python engine:

import sys 
help(sys) 
sys.version 
Like R, Python also includes many additional libraries. However, for the code shown in this chapter, you don't need to download anything. All necessary libraries for a data science project are already included in SQL Server Python installation.

As in any serious programming language, you can encapsulate your code inside a function. You define a function with the def name(): command. Functions can use arguments. Functions can also return values. The following code defines three functions, one that has no arguments, one that has two arguments, and one that has two arguments and returns a value. Note that there is no special ending mark of a function body—the correct indentation tells the Python interpreter where the body of the first function ends, and the definition of the second function starts:

def p_n(): 
    print("No args...") 
def p_2(arg1, arg2): 
    print("arg1: {}, arg2: {}".format(arg1, arg2)) 
def add(a, b): 
    return a + b 

When you call a function, you can pass parameters as literals, or through variables. You can also do some manipulation with the variables when you pass them as the arguments to a function. The following code shows these possibilities:

p_n() 
p_2("a", "b") 
# Call with variables and math 
a = 10 
b = 20 
p_2(a / 5, b / 4) 
add(10,20)

You can make branches in the flow of your code with the if..elif..else: statement. The following code shows you an example:

a = 10 
b = 20 
c = 30 
if a > b: 
    print("a > b") 
elif a > c: 
    print("a > c") 
elif (b < c): 
    print("b < c") 
    if a < c: 
        print("a < c") 
    if b in range(10, 30): 
        print("b is between a and c") 
else: 
    print("a is less than b and less than c") 

The results of the preceding code are:

    b < c
    a < c
    b is between a and c
  

The simplest data structure is the list. A Python list is a set of comma-separated values (or items) between square brackets. You can use a for or for each loop to iterate over a list. There are many methods supported by a list. For example, you can use the list.append() method to append an element to a list. The following code shows how to create lists and loop over them:

animals = ["cat", "dog", "pig"] 
nums = [] 
for animal in animals: 
    print("Animal: ", animal) 
for i in range(2, 5): 
    nums.append(i) 
print(nums) 

The result of the preceding code is:

    Animal:  cat
    Animal:  dog
    Animal:  pig
    [2, 3, 4]
  

Python also supports the while loop. The following example shows how to create a list from a string using the str.split() method, create a second list, and then use the while loop to pop an element from the second list and add it to the first list:

s1 = "a b c d e f" 
l1 = s1.split(' ') 
l2 = ['g','h','i','j','k','l'] 
while len(l1) <= 10: 
    x = l2.pop() 
    l1.append(x) 
    l1 

Here are the results:

    ['a', 'b', 'c', 'd', 'e', 'f', 'l']
    ['a', 'b', 'c', 'd', 'e', 'f', 'l', 'k']
    ['a', 'b', 'c', 'd', 'e', 'f', 'l', 'k', 'j']
    ['a', 'b', 'c', 'd', 'e', 'f', 'l', 'k', 'j', 'i']
    ['a', 'b', 'c', 'd', 'e', 'f', 'l', 'k', 'j', 'i', 'h']
  

The last data structure presented in this introduction section is the dictionary. A dictionary is a set of the key/value pairs. You can see an example of a dictionary in the following code:

states = { 
    "Oregon": "OR", 
    "Florida": "FL", 
    "Michigan": "MI"} 
for state, abbrev in list(states.items()): 
    print("{} is abbreviated {}.".format(state, abbrev)) 

The result of the previous code is:

    Oregon is abbreviated OR.
    Florida is abbreviated FL.
    Michigan is abbreviated MI.
  

I mentioned that in Python you can also use object-oriented paradigms. You can define a class with multiple methods, and then instantiate objects of that class. The instantiation operation creates an empty object. You might want to create objects with instances customized to a specific initial state. You can use a special method named __init__(), which is automatically invoked by the class instantiation. You can refer to an instance of the class itself with the self keyword. The following is an example of a class with two objects instantiated from that class:

class CityList(object): 
    def __init__(self, cities): 
        self.cities = cities 
    def print_cities(self): 
        for line in self.cities: 
            print(line) 
EU_Cities = CityList(["Berlin", 
                      "Paris", 
                      "Rome"]) 
US_Cities = CityList(["New York", 
                      "Seattle", 
                      "Chicago"]) 
EU_Cities.print_cities() 
US_Cities.print_cities() 

And here is the result:

    Berlin
    Paris
    Rome
    New York
    Seattle
    Chicago
  

Going deeper into object-oriented programming with Python is beyond the scope of this book. You will learn more about the data science tasks instead—data manipulation, creating graphs, and using advanced analytical methods to get some information from your data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212