By the end of this course, learners will be able to write basic Python programs that use variables, data types, operators in a fun and simple way.
Comments
Comments are a way to add notes or explanations to your Python code. They are ignored by the interpreter and do not affect the execution of your program. However, they can make your code more readable and understandable for yourself and others. There are two types of comments in Python: single-line comments and multi-line comments. Here are some examples of how to write comments in Python code:
This is a single-line comment. It starts with a hash (#) symbol and ends at the end of the line.
You can write anything you want here, such as jokes, insults, or secrets.
# The Corgicode is so awesome, it makes me cry.
# Or
# This code is so bad, it makes Corgi cry.
This is a multi-line comment. It starts and ends with three double quotes (“””) or (”’).
You can write anything you want here, as long as it is within the quotes.
You can use this type of comment to write longer explanations, descriptions, or documentation.
"""
This function calculates the area of a circle given its radius.
It uses the formula: area = pi * radius ** 2
It returns the area as a float value.
"""
Or:
'''
This is a secret message for you.
Do not share it with anyone else.
The password is: PythonIsFun
'''
Variables
Variables are a way to store and manipulate data in Python. They are like containers that hold values that you can use and change in your code. Here are some examples of how to write variables syntax in Python in a funny way:
To create a variable, you need to give it a name and assign it a value using the equal (=) sign.
name = "Cool Corgi" # This creates a variable called name and assigns it the value "Cool Corgi".
You can use any name you want for your variables, as long as they follow some rules:
- They must start with a letter or an underscore (_), not a number or a symbol.
- They can only contain letters, numbers, and underscores, no spaces or other symbols.
- They are case-sensitive, meaning that Name and name are different variables.
- They cannot be the same as Python’s reserved words, such as True, False, None, etc.
_name = "Vince Nguyen" # This is a valid variable name.
1name = "Eve" # This is an invalid variable name, because it starts with a number.
name! = "Adam" # This is an invalid variable name, because it contains a symbol.
True = "God" # This is an invalid variable name, because it is a reserved word.
To use a variable, you just need to write its name in your code.
Python will replace it with its value when it runs your code.
print(name) # This will print "Cool Corgi" to the screen.
You can also perform operations on variables, such as arithmetic, comparison, or concatenation.
age = 5 # This creates a variable called age and assigns it the value 5.
print(age + 5) # This will print 10 to the screen.
print(age > 18) # This will print False to the screen.
print(name + " is " + str(age) + " years old.") # This will print "Cool Corgi is 5 years old." to the screen.
To change the value of a variable, you just need to assign it a new value using the equal (=) sign again.
Python will update the value of the variable in your code.
name = "Cool Corgi" # This creates a variable called name and assigns it the value "Cool Corgi".
print(name) # This will print "Cool Corgi" to the screen.
name = "Vince" # This changes the value of the variable name to "Vince".
print(name) # This will print "Vince" to the screen.
Data types
Data types are a way to classify the values that you can use and manipulate in Python. They tell Python how to store, process, and display the data. There are many data types in Python, but here are some of the most common ones:
- int: This is a data type that represents whole numbers, such as 1, 2, 3, etc. You can use int values to perform arithmetic operations, such as addition, subtraction, multiplication, and division.
corgi_age = 5 # This creates a variable called corgi_age and assigns it an int value of 5.
- float: This is a data type that represents decimal numbers, such as 1.5, 2.7, 3.14, etc. You can use float values to perform arithmetic operations as well, but they are less precise than int values.
corgi_weight = 3.14 # This creates a variable called corgi_weight and assigns it a float value of 3.14.
- str: This is a data type that represents text or characters, such as “Hello”, “Python”, “Corgi”, etc. You can use str values to store and manipulate words, sentences, names, etc. You need to enclose str values in single quotes (‘ ‘) or double quotes (” “).
corgi_quote = "Corgi wana have fun in coding" # This creates a variable called corgi_quote and assigns it a str value of "Corgi wana have fun in coding".
- bool: This is a data type that represents logical values, such as True or False. You can use bool values to make decisions based on conditions, such as if-else statements.
is_corgi_funny_programmer = True # This creates a variable called is_corgi_funny_programmer and assigns it a bool value of True.
Operators
Operators are a way to perform operations on values and variables in Python. They are like symbols that tell Python what to do with the data. There are many operators in Python, but here are some of the most common ones:
Arithmetic operators: These are operators that perform basic math operations, such as addition (+), subtraction (-), multiplication (), division (/), modulus (%), exponentiation (*), and floor division (//).
x = 2 + 3 # This assigns x the value of 2 plus 3, which is 5.
Assignment operators: These are operators that assign values to variables, such as equal (=), plus equal (+=), minus equal (-=), etc. They are like shortcuts that combine an arithmetic operator and an equal sign.
x += 5 # This is the same as x = x + 5, which adds 5 to the current value of x and assigns it back to x.
Comparison operators: These are operators that compare two values and return a bool value of True or False, such as equal (==), not equal (!=), greater than (>), less than (<), etc. They are used to check conditions and make decisions.
x == 10 # This checks if the value of x is equal to 10, and returns True or False.
Logical operators: These are operators that combine two or more bool values and return a bool value of True or False, such as and, or, and not. They are used to create complex conditions and make decisions.
x > 5 and x < 15 # This checks if the value of x is greater than 5 and less than 15, and returns True or False.