The print function is used to display results in Python. Here's how to use it:
print("Hello, World!") displays the string "Hello, World!"
print(5+3) displays the result of the arithmetic operation 5 + 3
print(5>3) displays the result of the comparison operation 5 > 3
You can also print the values of variables:
x = 5
y = 3
print(x + y) displays the result of 5 + 3
Parameters and arguments
You can specify additional behavior for the print function using parameters. Here
are some examples:
print("Hello,", "World!", sep=", ") displays the text "Hello," and "World!"
separated by a comma and a space.
print("Hello, World!\n") displays the text "Hello, World!" followed by a newline.
print("Hello, World!", end="") displays the text "Hello, World!" without a newline.
Packages and Code Organization
When working on larger projects, it's important to organize your code into modules
and packages to make it more manageable. Here's how to do it:
To create a module, save your Python code in a file with a .py extension.
To create a package, create a directory and put your modules in it.
To import a module or package, use the import statement.
Implementing a Game: User Makes a Guess
Here's how to create a simple guessing game where the user has to guess a number:
Generate a random number using the random module.
Ask the user to input their guess.
Check if the guess is correct or not using an if statement.
Implementing Emoji Converter with Dictionaries
Here's how to create an emoji converter using dictionaries:
Define a dictionary with emoji codes as keys and emoji characters as values.
Ask the user to input a message.
Replace emoji codes in the message with their corresponding characters using the
dictionary.
Arithmetic operations in Python
Python supports various arithmetic operations, including:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Exponentiation: **
Strings and Character operations
Python supports various string and character operations, including:
Concatenation: +
Repetition: *
Slicing: []
Length: len()
Formatting: .format() or f-strings
Random Module and its Methods
The random module provides various methods for generating random values. Here are
some examples: