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

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

Operators

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

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

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
True
>>> 4 > 10
False
>>> 3 >= 3
True
>>>
>>> 5 == 6
False
>>> 6 != 9
True

Functions

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)
9.0
>>> float(-99999)
-99999.0

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)
10
>>> int(3.25)
3

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()
True
>>> (10.1).is_integer()
False

Strings

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

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

Manipulating strings

Operators

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

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

Functions

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)
15

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

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

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()
'Michael'
>>> ("empire").capitalize()
'Empire'

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()
'Yes'

Leave a Reply

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