Python first practice

Python Shell

Open the Python Interactive Shell:

  • Mac: Open your terminal and run: python or python3, depending upon your installation.
  • Linux: Open your terminal and run: python
  • Windows: If you have only one version of Python installed simply run python. If you have both Python 2.7 and Python 3 installed, run python for Python 2.7 and/or py -3 for Python 3.

Windows users: Ensure that your account has administrative privileges: Run the command prompt at an elevated level by right-clicking the command prompt icon, and then selecting ‘Run as Administrator’.

The interactive Python Shell should open, and your command prompt or terminal window should look similar to this:>>>

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

While you’re here, you might as well run your first line of code…

Python 2:>>>

>>> print "Python is fun!"

Python 3:>>>

>>> print("Python is fun!")

If you’re running Python 2.x, the print command is a statement rather than a function in Python 3.x.

You should see:>>>

Python is fun!

And that’s it: You’ve just written your first Python program! Each time you entered a line, Python immediately executed the statement, displaying the value between the quotes.

Once done, you can use exit() to leave the Python Shell. Or:

  • Mac and Linux usersCtrl+D and then press Enter
  • Windows usersCtrl+D and then press Enter

Keep your terminal or command prompt open. We still have more to do! Let’s continue using the Shell as we run through the Python primitives.


Numbers can be integers, floating points, Booleans, or complex numbers. The former three are the most important:

  • Integers are whole numbers – 1222476-99999
  • Floats have decimal points – 1.02.2222.098476.1-99999.9
  • Booleans represent either True or False (or 1 or 0). They represent data that can only be one thing or another.

Manipulating numbers


You’ve seen operators before. They’re things like addition (or concatenation) and subtraction, just like you learned in Elementary school.>>>

>>> 2 + 3   # Addition
>>> num1 = 10
>>> num2 = 9.99
>>> num3 = num1 + num2
>>> num3
>>> 8 - 5   # Subtraction
>>> 2 * 6   # Multiplication
>>> 12 / 3  # Division
>>> 7 % 3   # Modulus (returns the remainder from division)
>>> 3 ** 2  # Raise to the power

Putting your pre-algebra skills to the test, let’s look at comparisons, which evaluate to boolean values – e.g., either True or False (or 1 or 0).>>>

>>> 2 < 5
>>> 4 > 10
>>> 3 >= 3
>>> 5 == 6
>>> 6 != 9


Python provides you with a number of built-in functions for manipulating integers. These are always available to you. Please note: These functions can be used on any data type. There are a number of modules available in the Python Standard Library as well, such as math. To use the functions associated with these modules, you’ll first have to import the module. More on this later. For now, let’s look at a few examples of built-in functions.

float() – given an integer, this function returns a float.>>>

>>> float(9)
>>> float(-99999)

int() – given a float, this function returns an integer. This function does NOT round the input up (ceiling) to the nearest integer – it simply throws out anything after the decimal point (flooring) and returns the number. So the input of 10.6 returns 10, NOT 11. Similarly, 3.25 returns 3.>>>

>>> int(10.6)
>>> int(3.25)

Data-type methods

Besides functions, there a number of data-type methods associated with each type of number.

float.is_integer() – tests if a float is finite.>>>

>>> (10.0).is_integer()
>>> (10.1).is_integer()


Strings are lines of text that are declared with single or double quotes:>>>

>>> simple_string = "hey!"
>>> simple_string
>>> "hello world!"
'hello world!'
>>> escaped = 'can\'t'
>>> escaped
>>> not_escaped = "can't"
>>> not_escaped

Manipulating strings


Like numbers, you can concatenate strings (string concatenation):>>>

>>> "happy" + " " + "birthday"
'happy birthday'
>>> "my name is " + "john"
'my name is john'


Let’s look at just a few functions that are good to use for string manipulation:

len() – given a string, this function returns the length of it.>>>

>>> len(name_string)

slice() – given a start and stop value, you can access a set of, or single, character(s).>>>

>>> print("Hello"[2])
>>> print("Hello"[3])
>>> print("Hello"[0])
>>> print("Hello"[0:2])

Data-type methods

While we’ll only touch on a few data-type methods, be sure to check out the full Python documentation as there are a number of important functions that you should be aware of.

string.capitalize() – returns the string with the first letter capitalized.>>>

>>> lower_case_string = "michael"
>>> lower_case_string.capitalize()
>>> ("empire").capitalize()

string.format() – you can easily format values into strings.>>>

>>> name = "John Doe"
>>> greeting = "My name is {}".format(name)
>>> greeting
'My name is John Doe'

string.strip() – returns the string with the leading and trailing whitespace removed.>>>

>>> are_you_happy = "     Yes      "
>>> are_you_happy.strip()

Leave a Reply

Your email address will not be published. Required fields are marked *