This article is a quick guide that goes over syntax and concepts taught in the Python 1 course.
Last updated - July 22, 2024
Table of Contents
Creating a Variable
Outputting to the Terminal
Basic Data Types
Getting User Input
Comments
Concatenation
Concatenating Numbers
Math Operations
String Methods
String Indexing
Checking Strings
Lists
Comparison Operators
Conditional Statements
For Loops
Creating a Variable
Variables are created (or declared) by typing in a name, following that with an equals sign, and inputting the data you would like the variable to hold.
my_variable = "hello"
my_second_variable = 25
Outputting to the Terminal
Python's built in print() function outputs information to the terminal.
print("hello")
Output: "hello"
Basic Data Types
String | A string is a sequence of characters, usually text, that is surrounded by quotation marks.
my_string = "This is a string!" |
Integer |
An integer is a number without a decimal. my_int = 55 |
Float |
A float is a number with a decimal. my_float = 55.60 |
Boolean |
Booleans are either true or false. They are both capitalized in Python. is_true = True |
Getting User Input
In this example, the user is prompted with the question "How are you today?" Their response is saved to the "user_response" variable.
user_response = input("How are you today?")
Comments
Comments improve readability of code without being read by the Python interpreter. Comments can be created by starting a line with #.
#This is one comment
#This is another comment
Concatenation
Concatenation is the combination of multiple strings to create a single string. The + symbol concatenates multiple strings into one. When concatenating strings, be sure to add spaces at the beginning or end of a string! Python doesn't automatically include spaces, so you will need to manually add them when concatenating.
print("This is my " + "concatenated" + " string!")
Output: "This is my concatenated string!"
Concatenating Numbers
Using the built in format() function, you are able to concatenate numbers and strings. A string will need to contain at least one set of {} brackets to use the format function.
my_string = "I want to eat {} cookies!"
print(my_string.format(10))
Output: "I want to eat 10 cookies!"
Multiple sets of {} brackets can be used to concatenate multiple numbers into a string.
my_string = "I have {} dogs and {} cats."
dogs = 3
cats = 2
print(my_string.format(dogs, cats))
Output: "I have 3 dogs and 2 cats."
Indexes can be added to the {} brackets to determine the order that the numbers will be added. Index counting always starts at zero.
my_string = "I have {1} sandwiches and {0} cookies."
first = 4
second = 2
print(my_string.format(first, second))
Output: "I have 2 sandwiches and 4 cookies."
Math Operations
+ |
The plus symbol is used to add two numbers. my_sum = 5 + 5 |
- |
The minus symbol is used to subtract one number from another. my_difference = 10 - 2 |
* |
The asterisk symbol is used to multiply one number with another. my_product = 10 * 2 |
/ |
The forward slash symbol divides one number from another and returns a float. my_quotient = 10 / 2 |
// |
The double forward slash symbol divides one number from another and returns an integer. my_quotient = 10 // 2 |
% |
The modulo operator returns the remainder from when two numbers are divided. my_modulo = 10 % 6 |
String Methods
string.lower() |
The .lower() method makes all characters lowercase in a string. my_str = "HELLO" |
string.upper() |
The .upper() method makes all characters uppercase in a string. my_str = "hello" |
string.replace() |
The .replace() method replaces an inputted character with a second inputted character. my_str = "Good morning!" |
string.strip() |
The .strip() removes any extra spaces before or after a string. my_str = " Hello! " |
string.split() |
The .split() method splits a string into a list. Each word becomes its own list item. my_str = "This is my string!" |
string.find() |
The .find() method returns the lowest index that the inputted substring can be found. If the substring cannot be found, -1 is returned. my_str = "Good morning everyone!" |
string.rfind() |
The .rfind() method returns the highest index the inputted substring can be found. If the substring cannot be found, -1 is returned. my_str = "There are ducks over there" |
len(string) |
The len() method returns the length of a string. my_str = "Hello world!" |
String Indexing
string[index] |
This method returns the letter or symbol located at the entered index. An index number replaces the word index. my_str = "Hello world!" |
string[starting index : ending index] |
This method returns a slice of a string starting at starting index and ending one index position before ending index. my_str = "Hello world!" |
string[len(string) :: -1 ] |
This method returns a reversed string. my_str = "Hello world!" |
Checking Strings
To check if a substring exists in a string we use the in keyword. If the substring exists, True is returned, otherwise False is returned. (Keywords are reserved words in a programming language that are used in built in functions.)
string1 = "I love coding!"
check = "love" in string1
print(check)
Output: "True"
To check if a substring is not in a string, we use the phrase not in. If the substring does not exist in the string, True is returned, otherwise False is returned.
string1 = "I love coding!"
check = "love" not in string1
print(check)
Output: "False"
Lists
A list is a group of data that is ordered and changeable. Lists are surrounded by [] brackets and each list item is separated by a comma.
my_list = [1, 3, 5, 7, 9]
Lists can contain a number of different data types!
my_list = [True, 11, "lunch", 89, False, "apple"]
Comparison Operators
These operators are used when comparing items in a conditional statement.
> | The greater than operator checks if one item is greater than another. |
< | The less than operator checks if one item is less than another. |
>= | The greater than or equal to operator checks if an item is greater than or equal to another. |
<= | The less than or equal to operator checks if one item is less than or equal to another. |
== | The equal operator checks if two items are equal. |
Conditional Statements
The if statements run a block of code if a condition is True.
if 10 > 5:
print("Ten is greater than five!")
Output: "Ten is greater than five!"
The else statement runs a block of code if a condition is False.
if 5 > 10:
print("Five is greater than ten!")
else:
print("Five is not greater than ten!")
Output: "Five is not greater than ten!"
The elif statement runs if the initial if statement is False, but a second conditional statement is True.
if 5 > 10:
print("Five is greater than ten!")
elif 10 > 5:
print("Ten is greater than five!")
Output: "Ten is greater than five!"
To run a block of code if two statements are both True, you will use the and keyword.
if 5 < 10 and 20 > 10:
print("Both are true!")
Output: "Both are true!"
To run a block of code if at least one statement is True, you will use the or keyword.
if 5 < 10 or 10 > 20:
print("At least one condition is true!")
Output: "At least one condition is true!"
For Loops
For loops address each piece of data in a sequence.
desserts = ["cake", "ice cream", "pie", "brownies"]
for x in desserts:
print(x)
Output: "cake"
"ice cream"
"pie"
"brownies"
Code can be applied to each item in a list when using for loops.
desserts = ["cake", "ice cream", "pie", "brownies"]
for x in desserts:
print("I love to eat " + x)
Output: "I love to eat cake"
"I love to eat ice cream"
"I love to eat pie"
"I love to eat brownies"