Basic Commands in Python
To begin with, Python provides many basic commands to use data, perform calculations, and input/output manipulations. Finally, we cover so-called Magic Commands: a set of commands mostly used inside Jupyter Notebooks to simplify certain tasks.
Basic Python Commands
1. Printing Output
The print() function is one of the simplest and most used commands in Python. It allows us to display text or data on the screen.
print("Hello, World!")
Output:
Hello, World!
2. Comments
Comments are not executed by Python; they are used to add explanations or notes about the code.
# This is a single-line comment
print("This will run!") # Inline comment
Output:
This will run!
3. Variables
Variables are placeholders for storing data. You don’t need to declare their type explicitly in Python.
name = "Alice"
age = 25
print("Name:", name)
print("Age:", age)
Output:
Name: Alice
Age: 25
4. Data Types
Python supports multiple data types such as integers, floats, strings, and booleans.
x = 10 # Integer
y = 3.14 # Float
z = "Python" # String
is_cool = True # Boolean
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'str'>
print(type(is_cool)) # <class 'bool'>
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
5. User Input
The input() function reads data as a string entered by the user.
user_name = input("Enter your name: ")
print("Hello, " + user_name)
Example Interaction:
Enter your name: John
Hello, John
6. Conditional Statements
Control the flow of logic based on conditions using if, elif, and else.
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Output:
You are an adult.
7. Loops
Loops allow us to repeat a block of code.
For Loop:
for i in range(5): # Iterates from 0 to 4
print(i)
Output:
0
1
2
3
4
While Loop:
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
8. Functions
Functions encapsulate reusable logic.
def greet(name):
return "Hello, " + name
print(greet("Alice"))
Output:
Hello, Alice
9. Lists
Lists are collections of ordered, changeable items.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Adds an item
print(fruits[0]) # Access the first item
print(fruits) # Prints the entire list
Output:
apple
['apple', 'banana', 'cherry', 'orange']
Magic Commands in Python (Jupyter Notebook)
Magic commands are specific to Jupyter Notebooks and can greatly improve productivity by handling common tasks efficiently.
1. %pwd
Displays the current working directory.
%pwd
Output:
/path/to/current/directory
2. %ls
Lists files and directories in the current folder.
%ls
Output:
file1.py file2.txt notebook.ipynb
3. %time
Measures the execution time of a single line of code.
%time sum(range(1000000))
Output:
CPU times: user 12 ms, sys: 0 ns, total: 12 ms
Wall time: 11 ms
499999500000
4. %%time
Measures the execution time for a block of code.
%%time
total = 0
for i in range(1000000):
total += i
Output:
CPU times: user 145 ms, sys: 2 ms, total: 147 ms
Wall time: 146 ms
5. %run
Runs a Python script inside the notebook.
%run script.py
Output: (Depends on the content of script.py)
6. %matplotlib
Configures how Matplotlib displays plots.
%matplotlib inline
Output: (Plots will appear inline in the notebook.)
7. %history
Displays a list of previously executed commands.
%history
Output:
1: print("Hello, World!")
2: %pwd
3: sum(range(100))
...
8. %debug
Opens the debugger after an error.
# Example of an error
%debug
Output: (Debugger interface opens for error inspection.)
9. %who
Lists all variables currently defined.
%who
Output:
name age fruits
10. %%writefile
Writes content to a file.
%%writefile example.txt
This is a test file written using a magic command.
Output:
Writing example.txt
Advantages of Magic Commands
- Simplify workflow in Jupyter Notebooks.
- Don’t need other tools for frequently needed tasks.
- Integrated debugging and profiling tools.