iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

Python for Beginner Programmers

104 13

    Variable Assignment

    • Variable assignment in Python resembles assignment in other languages:

      x = 4

      In this example, the variable "x" now contains the value 4. Unlike some other languages, Python lines are not semicolon terminated. The end of a line signifies the end of a command. Furthermore, Python variables are not typed. Unlike C/C++ or Java, which uses variable declaration with types such as:

      int x = 4

      Python variables can take any value, and type is determined on variable assignment. When performing operations -- such as addition -- on variables, however, they must be of the same type. A programmer cannot add an integer and a string of characters, for example.

    Data Types

    • Python includes all the typical data types present in other languages, such as integers, floats, booleans and strings. Another important data type in Python is the List. A list is a collection of data items under one name, which can be referenced by index. For example,

      x = ['g', 't', 'e']

      The variable "x" is a list of three values. A list is denoted by square brackets around assigned values. To get any of these values, the programmer must reference is by index:

      print x[1]

      t

      For lists, all indices start at 0. The first index, x[0], will hold value "g" and so on.

    Function Definition and Calling

    • Functions take blocks of code and define them under a name, which a programmer can call to perform the code defined in the function at any time. The "def" keyword signifies a function definition:

      def double(x):

      --->return x*2

      The function "double" defines a block of code, and gives back an value (x*2). Using the function is a simple as using its name:

      y = 3

      z = double(y)

      print z

      6

      Blocks of code that remain together are signified by line indentation. The indentation is demonstrated with the arrow in this example. For code to remain part of the double function, it must indent one tab further after the double definition.

    If...Else

    • Python uses a few statements to control the flow of a program. The "if" statement performs an action based on a conditional or an evaluation of a value.

      if x == 2:

      /*do stuff*/

      The statement "x == 2" returns a value evaluated as either True or False. If True, the if statement performs its code. If False, the code is skipped. An "else" statement can define alternative code to an if, that will always execute should an "if" statement fail:

      if x==2:

      /*do stuff*/

      else:

      /*do other stuff*/

    While and For Loops

    • Similar to an if statement, a "while" statement creates a loop, or block of code that repeats as long as a conditional statement is true:

      while x==2

      /*do stuff*/

      If x does equal 2, then the code in the loop begins. Once it ends, the x==2 conditional is checked again, and if still true, the code will run again. The code in the while block will continue to run until x==2 returns a false value.

      "For" loops are special loops that work on lists. A for loop takes each item from the list and allows the programmer to do something with the values:

      x = ['g', 't', 'e']

      for y in x:

      print y

      In the for loop, the variable "y" will represent each element in the list, one at a time.

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.