Python

Python Menu

Variables

The ability to manipulate variables is one of the most powerful features of a programming language. A name that refers to a value is called a variable.

Assignment statements

An assignment statement creates a new variable and gives it a value:

>>> my_text = 'And now for something completely different'
>>> x = 17
>>> pi = 3.1415926535897932

This example makes three assignments. The first assigns a string to a new variable named my_text; the second gives the integer 17 to x; the third assigns the (approximate) value of π to pi.

Variable names

Programmers generally choose meaningful names for their variables, documenting what the variable is used for.

The names of variables can be as long as you like. Both letters and numbers may be included, but they can't start with a number. Using upper case letters is legal, but using only lower case for variable names is conventional.

The underscore character, _, can appear in a name. It is often used in names with multiple words, such as your_name or airspeed_of_unladen_swallow.

If you give a variable an illegal name, you get a syntax error:

>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax

76trombones is illegal because it begins with a number. more@ is illegal because it contains an illegal character, @. But what’s wrong with class?

It turns out that class is one of Python’s keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.

Python 3 has these keywords:

False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise

Expressions and statements

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions:

>>> 42
42
>>> n
17
>>> n + 25
42

The interpreter evaluates it when you type an expression at the prompt, which means that it finds the value of the expression. n has a value of 17 in this example, and n + 25 has a value of 42.

A statement is a unit of code that has an effect, like creating a variable or displaying a value.

>>> n = 17
>>> print(n)

The first line is an assignment statement that gives a value to n. The second line is a print statement that displays the value of n.

When you type a statement, the interpreter executes it, which means that it does whatever the statement says. In general, statements don’t have values.

Data Types

Numbers

Two types of numbers are supported by Python - integer(whole numbers) and floating point numbers (decimals).

To define an integer, use the following syntax:

myint = 7
print(myint)

To define a floating point number, you may use one of the following notations:

myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)

Strings

Strings are defined either with a single quote or a double quote.

mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)

The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)

mystring = "Don't worry about apostrophes"
print(mystring)

In defining strings, there are additional variations that make it easier to include items such as carriage returns, backslashes, and Unicode characters.

Simple operators can be executed on numbers and strings:

one = 1 two = 2 three = one + two print(three)
hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld)

Assignments may be performed on more than one variable on the same line "simultaneously" like this.

x, y = 1, 5 print(x,y)

Mixing operators between numbers and strings is not supported:

# This will not work one = 1 two = 2 hi = "hi"
print(one + two + hi)

Exercise

Creating a string, an integer, and a floating point number is the aim of this exercise. The string should be renamed my_string, containing the word 'hello.' The number of the floating point should be called my_float, containing the number 12.5, and the number of the integer should be called my_int, containing the number 210.

# change this code my_string = None my_float = None my_int = None
# testing code if my_string == "hello": print("String: %s" % my_string) if isinstance(my_float, float) and my_float == 100.0: print("Float: %f" % my_float) if isinstance(my_int, int) and my_int == 250: print("Integer: %d" % my_int)
# change this code my_string = "hello" my_float = 12.5 my_int = 210
# testing code if my_string == "hello": print("String: %s" % my_string) if isinstance(my_float, float) and my_float == 12.5: print("Float: %f" % my_float) if isinstance(my_int, int) and my_int == 210: print("Integer: %d" % my_int)
test_object('my_string', incorrect_msg="Don't forget to change `my_string` to the correct value as per the instructions.") test_object('my_float', incorrect_msg="Don't forget to change `my_float` to the correct value as per the instructions.") test_object('my_int', incorrect_msg="Don't forget to change `my_int` to the correct value as per the instructions.") test_output_contains("String: hello",no_output_msg= "Make sure your string matches exactly as per the instructions.") test_output_contains("Float: 12.500000",no_output_msg= "Make sure your float matches exactly as per the instructions.") test_output_contains("Integer: 210",no_output_msg= "Make sure your integer matches exactly as per the instructions.") success_msg("Great job!")

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods