Part 2: Basic Syntax
Topic 1: Calculations, Comments, Assign Variables, Data Types
Calculations
Python like most high-level programming languages (Java, ruby, JavaScript, C++, C#) can work as a calculator. Type some of these in your python shell to see some of the outputs.
Operation |
Output |
x + y |
Sum of x and y |
x - y |
Difference of x and y |
x * y |
Product of x and y |
x / y |
Quotient of x and y |
x // y |
Rounded down quotient of x and y |
x % y |
Remainder of x and y |
x ** y |
x to the y power |
Comments
Comments are important parts of code they help you and others understand what your code is doing. It’s extremely helpful when you start writing functions and need help understanding the connections in your code. To write a comment in python you use the # symbol at the beginning of the line. In python the line will turn green and you’ll be able to type anything you want after.
If you want more than one line of code commented out you can use “”” …. “””
three quotation marks at the start and end to have a set of lines commented fully out.
>>> # This would be a comment line if you were coding right now
’’’
Also
This
Would
Too
‘’’
Variables
Variables are placeholders of information in the memory. It’s used in sort of the same way as in algebra
>>> A = 12
>>> 10 + A = 22
In programming a variable is used to access and manipulate data. A variable is a name that represents a piece of information in the computer’s memory. In python you create and set variables. In python to create a variable you use the = sign.
>>> Height = 20
To use the variable you can substitute it into any of the calculations from earlier.
>>> 12 + height = 32
Variable Naming rules:
- You can’t use one of python’s key words as a variable name
- You can’t have spaces in your variable name
- The first character of a variable must be a letter or an underscore
- Python is case sensitive X is different than x
When creating variables it’s good practice to name the variable so it reflects the purpose of the variable. Programmers also tend to use camel case or underscores to denote different words so a variable called grosspay would look like gross_pay or grossPay .:
Variables can hold a lot more than just numbers. In programming variables can hold a multitude of information including (strings, numbers, data frames, Booleans, floats, lists)
# Create a variable and display it
first_name = “James” # To denote words to a variable you will have to use quotation marks
last_name = “Smith”
# display the values
Print(first_Name, last_Name)
Beginner Data Types
Data Type: |
|
integer |
X = 4 | Regular numbers are considered integers |
string |
X = “Hello There” |
list |
X = ((“dog”, “cat”, “fish”)) |
Boolean |
X = True | This holds two values true or false |
float |
X = 4.2 | these are numbers specified with decimals |
Topic 2: Data Types Explained
Strings
A string in programming is a sequence of character information. In python a string is denoted as str. You can create strings use single or double quotation marks “, ‘ try doing this code in your shell.
>>> Print(“hello”)
OUTPUT: hello
Or
>>> Type(“hello”)
OUTPUT: char
You can also combine strings using the addition operator ( + ) but you can only add strings to strings and not to other data types.
>>> X = “ hello “
>>> Print(“Are you ok “ + x)
This would work
But
X = 4
Print(“Are you ok “ + x)
would not work
X = 4
Print(“Are you ok”, x)
This would work in python and would append x onto the end of the sentence
You can’t add strings and integers together you have to convert x to a strong before you do the operation. To convert a variable you can use the string function.
>>> Str(x)
Lists
To create an empty list you use:
>>> List = []
A list is a data type that can hold multiple pieces of information. A list can also hold multiples of the other data types.
>>> List = ["chicken", "shrimp". "beef"]
>>> Print(list)
OUTPUT: ["chicken”, "shrimp". "beef"]
You can call specific items in the list by using index
Print(list[0])
OUTPUT: chicken
Make sure to note that indexes start at 0 and not 1. This will be important for all of python. You can also edit parts of the list.
List[1] = “Lamb”
Print(list)
OUTPUT: ["chicken", "lamb". "beef"]
Floats
Floats work the same as integers but they can contain decimals for example
>>> type(1) # This code will output float
Output: <class "float”>
>>> type(1.0) #This will output decimal
Booleans
Booleans hold two values True and False it’s mostly used to indicate if a condition is met or not once loops come into play these will be more important.
To create a Boolean you set the variable equal to True or False (Remember python is case sensitive)
- abs() - a function to evaluate the absolute value of a number. Try abs(-5) and see what you get.
- max() - a function to evaluate the maximum of a list of values
- min() - a function to evaluate the minimum of a list of values
- sum() - a function to evaluate the sum of a list of values
- len() - a function to evaluate the length of an object
Topic 3: Inputs
Sometimes you’ll want to get some keyboard output or easily change the variables in your data. Python has a function that allows you to get keyboard output from the user. :
Getting keyboard input is useful when you don’t want to hard code numbers into your program.
Putting all the information here together you should be able to:
- Create a variable
- Assign it to different data types
- Create a list, int, float, list, and Boolean
- Print out and combine phrases, numbers, and inputs