01 Hello Python

Python Basics Part I

Ask Python to print out a Variable

You may easily ask Python to print out a Variable on screen by using the print() function. Have a try in the interactive boxes below:

# Create a variable a, equal to "Hello Python!" # Print out a # Create a variable a, equal to 5 a = "Hello Python!" # Print out a print(a) test_object("a") test_function("print") success_msg("Great job!")
Use the assignment operator (=) to create the variable a.

Python Variables

Python Variable Types

This tutorial will go over a few basic types of variables.

Numbers
Python supports two types of numbers - Integers and Floating point numbers. (It also supports complex numbers, which will not be explained in this tutorial).

To define an integer, use the following code, and you can use type(varialbe_name)function to get the type of variable

# Create a variable a, equal to 5 # Print out the type of variable a # Create a variable a, equal to 5 a = 5 # Print out a print(type(a)) test_object("a") test_function("type") Ex().check_object('a').is_instance(int) success_msg("Great job!")
Use the function (type()) to get the type of variable a.

To define a float, use the following syntax:

# Create a variable a, equal to 5.0 # Print out type of a # Create a variable a, equal to 5.0 a = 5.0 # Print out a print(type(a)) test_object("a") Ex().check_object('a').is_instance(float) test_function("type") success_msg("Great job!")
Use the function(type()) to get the type of the variable a.

In addition to integers and floats, Python have many other embedded variable types, including String (We have used that one in our first example), Boolean (True or False)

# Create a boolean variable a and set it to be True (note NO quotation marks!) # Print out type of a # Create a variable a, equal to 5.0 a = True # Print out a print(type(a)) test_function("type") Ex().check_object('a').is_instance(bool) Ex().check_object('a').has_equal_value() success_msg("Great job!")
Use the function(type()) to get the type of the variable a.

Python Variables Naming Rules

  • Variables names must start with a letter or an underscore, such as:
    • _underscore
    • underscore_
  • The remainder of your variable name may consist of letters, numbers and underscores.
    • password1
    • n00b
    • un_der_scores
  • Names are case sensitive. The following are different!
    • case_sensitive
    • CASE_SENSITIVE
    • Case_Sensitive

Python Variables Operations

This section explains how to use basic operators in Python.

Arithmetic Operators

  • +: Plus
  • -: Minus
  • *: Multiply
  • /: Divide
  • %: Reminder
  • **: Power
There are many ways to use the operators in a creative way

First think about potential print outs before you run the code and then compare yours with the outputs

print("Basic Math Claculation") a = 1 + 2 * 3 / 4.0 b = 1 + 2 * 3 / 4 print("a equals to %s and b equals to %s" %(a,b)) print("the number will be different if you are using Python 2.7") print("-"*30) print() print("Claculate Reminder") remainder = 11 % 3 print("The reminder is %s"%remainder) print("-"*30) print() print("Power") squared = 2 ** 2 cubed = 2 ** 3 print("square of 2 is %s and cube of 2 is %s"%(squared, cubed)) print("-"*35) print() print("Operation with string") helloworld = "hello" + " " + "world" + " " print(helloworld) print() lotsOfHelloWorld = helloworld * 5 print(lotsOfHelloWorld) # Beaware of the data type a = 1 + 2 * 3 / 4.0 b = 1 + 2 * 3 / 4 # Print out results print("a equals to %s and b equals to %s" %(a,b)) print("the number will be different if you are using Python 2.7") success_msg("Great!")
First think about potential print outs before you run the code and then compare yours with the outputs