In this first post, I'll provide some tips for beginners in the Python world, covering the main useful commands that are simple to use in exploratory analysis.
Comments:
Use the "#" character to start a comment on a line. Comments are useful for adding explanatory notes to your code and are not executed by the Python interpreter.
# This is a comment
Line Breaks:
To split code across multiple lines, you can use the backslash "" at the end of each line or place it between parentheses, brackets, or braces.
# Using backslash
x = 10 + \
20 + \
30
# Using parentheses
y = (10 +
20 +
30)
# Using brackets
lista = [1, 2,
3, 4]
# Using braces
dicionario = {'a': 1,
'b': 2}
Indentation:
Python uses indentation to delimit code blocks, instead of using curly braces or special keywords. Make sure to maintain the same indentation within a block to avoid syntax errors.
# Example of indentation
if x > 0:
print("x is positive")
print("Still inside the block")
print("Outside the block")
Printing to the Screen:
Use the print()
function to display messages or values on the standard output.
name = "Ana"
print("Hey there,", name)
x = 42
print("The secret value of x is:", x)
User Input:
Use the input()
function to receive user input. Remember that the result of input()
is always a string, so you might need to convert it to other types if necessary.
name = input("What's your name? ")
print("Welcome,", name)
Assignment Operators:
Python provides several useful assignment operators to perform common operations in a single line.
x = 10 # Simple assignment
x += 5 # x = x + 5
x -= 3 # x = x - 3
x *= 2 # x = x * 2
x /= 4 # x = x / 4
Importing Libraries:
Use the import
keyword to import libraries and modules into your code. This allows you to access additional resources and functions provided by these libraries.
import math
x = math.sqrt(25)
print(x)
from datetime import datetime
now = datetime.now()
print(now)
Shape
Returns a tuple representing the number of rows followed by the number of columns in a dataset:
df.shape
DataFrame Columns
Returns all columns, separated by commas, that compose a dataset. Very useful to recall which columns make up the dataset after transformations or even during exploratory analysis:
df.columns
Top comments (0)