Bertelsmann Tech Scholarship Challenge, Python, Udacity

Python Basics

Print Statement

print("Hello World!")

This will output whatever is inside the ( ) to the screen/file.

To output a string make sure that it’s inside ” ” or ‘ ‘.  Everything inside the brackets will be printed out in order.

To be best practice don’t put unnecessary spaces in the statement. The code will run but it’s not what is expected of us as programmers.

print ("Hello World")

print(   "Hello World!"   )

Syntax Errors

They are very common when typing in code. Computers are fast, but they are also stupid. If there are any tiny mistakes in the code, the computer will panic and produce an error message.

The best way to get through them is to take the time and read each line of code and make sure that all the quotes and parentheses are closed.

Escape Sequences

They have a special purpose. They can be used in text to format it. The backslash is used to ‘escape’ characters that are used in Python. When we want to print some text to the screen we wrap it in speech marks. Which brings us an issue when we want to add speech marks in our output. The solution to this problem is to put a backslash before it.

print("This is a great course.\n \"I'm learning lots\" was heard in the chat rooms")

\n — Creates a line return in a string of text.

\t — Creates a tab style indent in a string of text

\\ — Allows a backslash to appear in a string of text

\” — Allows a speech mark to be used in a string of text

Math

Using Python as a calculator is easy. 

Symbols

  • Addition:  +
  • Subtraction: –
  • Multiplication: *
  • Division: /
  • Remainder: % (named modulus)

There are two types of numbers

  • Whole numbers  which are referred to as Int.
  • Decimals which are referred to as float. 

Combining Text and Math

It is possible to combine text (string) and numbers in the print() function. The comma is there to separate text from math or we can convert the math into a string.

print("205 divided by 5 = ", 205/5)

or 

print("205 divided by 5 = " + str(205/5))

Output: 205 divided by 5 = 41

Operators

There are several operators that we can use with numbers besides the calculator symbols.

  • ==     equal to
  • !=     not equal to
  • >     greater than
  • <     less than
  • >=    greater than or equal to
  • <+    less than or equal to

NOTE: We use a double sign to compare two values and a single equals to assign a value to a variable.

Variables

Variables are like containers that store data that we assign to them. To assign something to a variable we do this by creating the variable and then assigning a value. The value can be text, number or boolean

first_name = "Claudia"

my_age = 39

NOTE: Although we can do “39” for the age we don’t want to use it as string in case we want to do some kind of mathematical operation to is so we assign it without the ” “.

When creating variables we want to:

  • Only use ordinary letters, numbers and underscores in your variable names. They can’t have spaces, and need to start with a letter or underscore.
  • You can’t use reserved words or built-in identifiers that have important purposes in Python.
  • The pythonic way to name variables is to use all lowercase letters and underscores to separate words.

Booleans

The bool data type holds one of the values True or False, which are often encoded as 1 or 0, respectively.

This is from Lesson 22:  Data Types and Operators Videos 1 – 18

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s