Basics, Solutions
Tip
Let’s make learning Python fun and practical!
Literals are fixed values in your code. They can be numbers, strings, booleans, etc.
Note
Examples: - 42
(integer) - 3.14
(float) - 'hello'
(string) - True
, False
(boolean)
Use literals to set default values in games, apps, or data analysis scripts!
Literals are fixed values written directly in your code and are used to assign values to variables or as standalone values in expressions.
"""
This example shows different types of literals.
"""
integer_literal = 42 # An integer literal
float_literal = 3.14 # A floating-point literal
string_literal = "hello" # A string literal
boolean_literal = True # A boolean literal
print(integer_literal, float_literal, string_literal, boolean_literal)
# Output: 42 3.14 hello True
This code demonstrates how to use different types of literals in Python.
Variables store data for use in your program which can be manipulated by operators (e.g., addition, comparison, and assignment).
"""
This example shows how to use variables and operators.
"""
x = 10 # Assign 10 to x
y = 5 # Assign 5 to y
sum_xy = x + y # Addition operator
diff_xy = x - y # Subtraction operator
prod_xy = x * y # Multiplication operator
is_equal = x == y # Comparison operator
print(sum_xy, diff_xy, prod_xy, is_equal)
# Output: 15 5 50 False
This code uses variables and operators to do math and compare values.
Loops repeat actions. Conditionals let your code make decisions.
This code loops through numbers and uses a conditional to check if each is even or odd.
Squaring means multiplying a number by itself. There are several ways to do this in Python.
"""
This example shows three ways to square a number.
"""
def square(n):
"""Returns the square of n."""
return n * n
num = 7
squ1 = num * num # Multiplication
squ2 = num ** 2 # Exponentiation
print(f"squ1: {squ1}, squ2: {squ2}, square(num): {square(num)}")
# Output: squ1: 49, squ2: 49, square(num): 49
This code demonstrates three ways to square a number in Python.
Strings store text. Slicing lets you extract parts of a string.
"""
This example slices a string in different ways.
"""
text = "Python is awesome!"
first_word = text[:6] # Get 'Python'
last_word = text[-8:] # Get 'awesome!'
every_other = text[::2] # Get every other character
print(first_word) # Output: Python
print(last_word) # Output: awesome!
print(every_other) # Output: Pto saeo!
This code shows how to slice strings to get different parts or patterns.
Try these programming challenges to practice your Python skills!
Write code that uses at least three different types of literals (integer, float, string, boolean) and prints them in a single sentence.
Create two variables, perform addition, subtraction, multiplication, and division, and print the results with clear labels.
Write a loop that prints numbers from 1 to 10. For each number, print whether it is a multiple of 3 or not.
Write a function that takes a number and returns both its square and its cube. Print the results for the number 5.
Given the string mystery = "QuartoPythonRocks!"
, print:
This code uses integer, float, string, and boolean literals in a sentence.
This code performs math operations and prints results with labels.
This code loops through numbers and checks for multiples of 3.
This function returns both the square and cube of a number.
This code slices the string in three different ways.