Python Basics Reference Guide

When you need a little extra help with Python basics, this article is a great resource!

Last updated - July 10, 2023

 

Python is a very popular back-end programming language that is often used for web development. Python works well with JavaScript, another popular programming language.

Python stands out among other programming languages because of its simple syntax and similarity to the English language. In order for your Python to run, first, make sure that you are writing your code in a python file .py.

Variables and data types

Variables are the building blocks of python and many other programming languages. They can be used to store objects (text, numbers, and more.) In python, you do not need to specify anything before declaring a variable. You just need to use the equal sign = to assign the data to the variable. You can name the variable anything you want and you can put any type of data inside. When you want to store a word or phrase inside a variable you need to put quotes around it. Numbers don't need any extra punctuation. Following are two examples of variables in python. One variable is for a name, and one is for age.

name = "Jamie"

age = 7

There are four basic data types you can assign to your variables: strings, integers, floats and booleans. 

Variable

A container that stores information.

Example:

height = 35

Strings  Text values. By wrapping a variable's value in quotes, you create a string. Example:

name = "Hannah"
Integers

Integer values are numbers, negative or positive. They are not wrapped in quotes. Example:

age = 24

Float

Floats are numbers, positive or negative, with one or more decimals.

age = 4.5

Booleans

A data type that represents the value of true or false. The first letter in True or False must be capitalized in python. Example:

answer = True

answer2 = False

Syntax is very important when declaring your python variables! The same data can be a different data type depending on the punctuation used. For example: 

"23" is a string

23 is an integer

23.0 is a float

Be mindful of the data types you are using!

Input & Output

Writing programs that can get input from the user is a fundamental skill for any programmer. When we receive input from a user, we always assign it to a variable. To get input from a user start by naming your variable. For example: first_name. Next, type the equal sign and the word input. Then you will put the question you want to ask the user, or the information you want to gather from them, in parentheses and quotation marks. For example: ("What is your name?") The complete code should look something like this: first_name = input("What is your first name?") This code asks the user to input their first name, and assigns the user's input to the variable first_name.

An output is something the computer generates from information it received. There are many kinds of outputs, but the main one used in Python is the print statement. To run a print statement you should type the word print, and then place the variable or words you want to print in parentheses. If you are typing in new words and not a variable you have already defined, you should use quotes. If you would like to print a sentence/string and include a variable you should add a plus sign between the string and the variable. For example:

print(first_name) will print the variable first_name, which is an input from the user.

print('Hello!') will print the sentence Hello! Notice that the sentence is surrounded by quotes.

print('Hello' + first_name + '!') will print Hello Hannah! (In this case, Hannah would be the name inputted by the user for the variable first_name.)

Input variable = input("Ask for input from the user here")
Output

print(variable)

print('string')

print('string' + variable)

How to Convert Data Types 

In python, you are able to change your variable from one data type to another. This can be very helpful when doing math in Python!

Converting is done by specifying the variable type when assigning a value to the variable.

Convert to an integer

int( )

*Changing a float to an integer will drop the decimal, not round down.

Convert to a float float( )
Convert to a string str( )
Convert an input

 Inputs are automatically accepted as a string.. To convert an input to an integer, you should wrap your input with int ( ).

Example:

variable = int(input("How old are you?"))

This will convert whatever input we receive from the user into an integer. This way, we can use their input for math.

 

Symbols for Python Math 

+ Use + for addition
- Use - for subtraction
* Use * for multiplication
/ Use / for division
%

Use %  for modulus.

The modulus returns the remainder if a number is divided.

Example: 10 % 3 = 1

10/3 is equal to 3 with a remainder of 1


Troubleshooting your code:

  • Are you in the correct file type? For python you should use a .py file.
  • Is your syntax correct in your Python file? 
    • Many languages end computer commands with semicolons, but python ends commands with a line break (pressing the enter key) instead.  Indentation is very important for Python syntax.
    • Make sure you have included parentheses ( ), quotation marks (" "), plus(+), and equal(=) signs where they are needed.
    • Check your spelling.
    • Check your spacing.
  • Do your variable types and values match?
    • Remember that string values should be wrapped in quotation marks " ".
    • Boolean values can only be True or False.
  •  Check and double-check your code!
    • It is easy to overlook a small mistake or typo! To prevent having to search through a long file for typos, try to check for mistakes and make sure your code works often while you build your website.
  • Read the lesson and requirements again.
    • If you are having trouble, it can help to read the lesson and requirements again. Make sure you understand the lesson and make sure your code includes all of the requirements that the challenge or checkpoint asks for. Sometimes, the autograder can be picky and wants your code to include the same words, spacing, and punctuation as the requirements or example.
    • As you go through Python lessons on Skill Struck, the checkpoints and challenges will be checking to see if your print statements match the requirements EXACTLY.  It needs to match capitalization, punctuation, and spacing.