How to Write Code in Python

Python is an interpreted programming language that focuses mostly on object-oriented and imperative programming styles for use on different operating systems. Python differs from some other programming languages in that it doesn't use brackets the same way, but does place strict rules on how you format the code. You can write code in a Python file to save it or enter it directly at the prompt on the Shell or command line.

Instructions

    • 1

      Open the Python Shell and start a new program file. Type the following:

      # This is my program

      print "Hello, world!" # My first line of code

      The hash symbol denotes a comment. Comments do not show up in the main program. They simply help you understand the code better, especially when you have hundreds or thousands of lines. You can place comments anywhere in the program file.

    • 2

      Type the following:

      strVar = "This is a string variable."

      intVar = 25

      These lines create two strings. Unlike some other programming languages, you do not need to explicitly declare the data type. Python sees the variable value and automatically assigns a data type. You can use other types, include floating point numbers and Boolean values.

    • 3

      Type the following:

      width = 15

      height = 32

      print width * height

      The first two lines creates two integer variables. The "print" function on the third line prints the multiplied value to the screen. You can use many other kinds of math.

    • 4

      Type the following:

      userSelection = raw_input("Make a choice (1 or 2): ")

      if userSelection == "1":

      --> print "You selected 1."

      elif userSelection == "2":

      --> print "You selected 2."

      else:

      --> print "You did not select properly."

      The "raw_input" function prompts the user to enter some text. The first line does so and saves the user's input in a variable. It then uses an "if" statement to inform the user of his selection. Note that Python does not use brackets like some other programming languages. However, indenting is important in Python. Whenever you use block code, such as with this if statement, you must indent the next line using either a space or a tab (shown here with "-->"). This also applies to other things such as loops, for statements and functions.

    • 5

      Type the following:

      a = 1

      while a <= 10:

      --> print a

      --> a = a + 1

      This prints out the numbers one to 10 in a loop, using only one variable and updating it on each pass.

    • 6

      Type the following:

      def myFunction():

      --> userText = raw_input("Type something: ")

      --> print "You wrote: " + userText

      The "def" keyword denotes that you are creating a new function. In this example, the function simply prompts the user for text, then prints that text to the screen. Again, note the indenting within the block of the function. To call this function, simply type "myFunction()" anywhere else in the program or run it from the Python Shell.

    • 7

      Type the following:

      list = ['a','b','c','d']

      list.append('e')

      print list

      print list[2]

      The first line creates a list of characters from 'a' to 'd.' The next line appends the character 'e' to the end of the list. Note that to create a list, you use square brackets, but to use the append method, you use round brackets. The third line prints the entire contents of the "list" list while the fourth line prints only the third value, 'c.' It prints the third value because Python uses zero-indexing, which means the first item in the list has index zero, the second item has index one, and so on. You may use different data types for each item in a list.

    • 8

      Save the Python program file and run it in the Shell.

Related Searches:

References

Comments

Related Ads

Featured