Edit Article

The SetupHello World!StringsConcatenating

Edited by Hou, Jonathan E., Sondra C, Nancy Shaw and 12 others

This will be a brief introduction to the Python programming language. This guide isn't meant to make you a "1337 l-l4l<3r". That would take years of experience. This intro will be just like the classes you took in high school that taught German/Spanish/French. As with any language (computer or verbal) Python will take time to learn. The amount of time it takes for you to learn it depends on how much time/energy you commit to it. Python is an excellent language to start learning, due to its simplicity.

Ad


EditMethod 1 of 4: The Setup

  1. 1
    Download the latest version of Python from here Python 3.2.3 is the most stable version thus far. Make sure you download the version for your OS, and follow the installation instructions.
    Ad
  2. 2
    Decide how you want to program in Python. You have two options: Interactive mode (basically just the cmd prompt for windows users) or an IDE (Integrated Development Environment) which is just a pretty version of interactive mode.
  3. 3
    Open the editor that you have chosen to use. The screen should display something very similar to the following.
  4. 4
    Notice the ">>>" at the bottom of the message. This prompt tells Python (and you) what follows next is the beginning of the code and this is the area which we will be entering commands.
Python 2.4 (#1, , June 28 2007, 09:18:58)
[GCC 3.4.1] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>>



EditMethod 2 of 4: Hello World!

  1. 1
    Type print "Hello, world!" at this prompt as seen below and press Enter and the program should respond to you by displaying Hello, world!
  2. 2
    Know that you've done a couple of things here. Firstly you've just created your first program. Simple huh? You've also just learned how to write a string.
>>>print "Hello, world!"
Hello, world!
>>>

EditMethod 3 of 4: Strings

  1. 1
    Take a crash course on strings:
    • Strings store test or binary data.
    • Strings are immutable (unchangeable).
    • A string literal is surrounded by quotation marks.
    • 'This is a string'<---This is the normal look to a string
    • "This is a string"<---This example (as well as the following one) are examples of escaping quotes
    • '''This is a string'''
    • All of these strings get treated the same when Python spits out the output:
      • 'This is a string'
    • The reason for the different ways to write a string is best posed as a question. If you wanted to write a string that contained an apostrophe/quotation marks how would you do it? Simple:
      • "Coding in Python isn't boring" <---Notice the ' for the contraction.
    • '''Scott likes to say, "This is a string"'''<---Notice the " around what Scott likes to say.
    • There is also another way to escape the issue's surrounding any ' within the string. Just put the backslash character (\) in front of the ':
      • "\"Hello, world!\", Scott said"<---Notice the locations of the backslash (and realize you've just used another escape character), they're before every "internal" "</nowiki>
  2. 2
    Type that at the prompt (>>>) and you'll see:
>>>"\"Hello, world!\", Scott said"
"Hello, world!" , Scott said
>>>

EditMethod 4 of 4: Concatenating

  1. 1
    In order to join two different strings to make one you need to concatenate (add them together):
    Ad
>>>'This is' + 'a string'
'This is a string'
>>>
  • Try assigning a variable. You can assign strings to variables, and most everything else as well:
>>>x = 'This is ' <-- note the space between the is and the '.
>>>y = 'another test'
>>>x + y
'This is another test'
>>>
  • Note that if you make a mistake and type in x = y instead of x + y it will assign 'another test' to the variable x as well. Keep in mind that you cannot concatenate an integer (number) with a string. That should be obvious right now, as you cannot add a sentence to a number...can you?


Edit Tips

  • If you fail, try again
  • You can also download wrappers and add-ons, like Pygame
  • If you have trouble using the IDLE when writing your programs. Try using notepad or another editor. To save your program add .py at the end of the name.
Ad

Article Info

Categories: Web Programming

Recent edits by: Xavier Combelle, Denise, Jack Herrick

In other languages:

Español: Cómo aprender el lenguaje Python

Ad

Thanks to all authors for creating a page that has been read 17,806 times.

Was this article accurate?

YesNo
x

Thank Our Volunteer Authors.

Follow us on Google+