DEV Community

Cover image for #01QuickTips: Python
Ana Carolina Branco Neumann
Ana Carolina Branco Neumann

Posted on

#01QuickTips: Python

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Shape

Returns a tuple representing the number of rows followed by the number of columns in a dataset:

df.shape
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Measure and Advance Your DevSecOps Maturity

In this white paper, we lay out a DevSecOps maturity model based on our experience helping thousands of organizations advance their DevSecOps practices. Learn the key competencies and practices across four distinct levels of maturity.

Get The White Paper

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →