Python scripting - Introduction

Tips for beginners

 

Tips

 

Editor


You can use any text editor for writing the python code.



Dynamic typing


In Java, C++, and other statically typed languages, you must specify the data type of the function return value and each function argument. On the other hand, Python is a dynamically typed language. In Python you never have to explicitly specify the data type of anything. Based on what value you assign, Python will keep track of the data type internally.



Python statements


Python uses carriage returns to separate statements, and a colon and indentation to separate code blocks. Most of the compiled programming languages, such as C and C++, use semicolons to separate statements and curly brackets { } to separate code blocks. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces.


[Example]

x = 1 

if x == 1: 

    # indented four spaces print 

    "x is 1." 



Operators == and =


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.



Concatenating strings


You can use ‘+’ to concatenate strings like so:


[Example]

print ‘Fast’+’Suite’

 

[Output]

FastSuite



The __init__ method


The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. The __init__ method is similar to a constructor in C++, C# or Java.


[Example]

class Person:

    def __init__(self, name):

        self.name = name

 

    def sayHi(self):

        print ‘Hello, my name is’, self.name

        p = Person(‘Fritz’)

        p.sayHi()

 

[Output]

Hello, my name is Fritz



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.


[Example]

# file my_function.py

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)



Lists


Python has a built-in list type. List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.

 

  speed = ['slow', 'normal', 'fast']

  print speed[0]    ## 'slow'

  print speed[2]    ## 'fast'

  print len(speed)  ## 3

 

Cycle is a way to look at each element in a list. Do not add or remove from the list during iteration.

 

  squares = [1, 5, 10, 16]

  sum = 0

  for num in squares:

    sum += num

  print sum  ## 32



Tuples


A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

 

Accessing Values in Tuples:

 

To access values in tuple, use the square brackets for slicing along with the index to obtain the value. For example:

 

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

 

print "tup1[0]: ", tup1[0]

print "tup2[1:5]: ", tup2[1:5]

 

When the above code is executed, it produces the following result:

 

tup1[0]:  physics

tup2[1:5]:  [2, 3, 4, 5]



Useful Python information




Previous
Page précédente
Chapter
Page principale du chapitre
Next
Page suivante