Python Syntax / Sintaxis de Python

Intro to Code A Progress
20%

What is syntax?

Syntax is the way that code is typed out or organized so that computers can understand what we want it to do. It’s like the English language, we have to form sentences in a specific way so that other people can understand us. In the following video, we will go more in detail the syntax of each of the elements listed below.

Functions

A function is a command you give to a computer. Each function needs to include parentheses at the end. The reason why functions end in parenthesis is that sometimes you can put things inside these parenthesis to give to the function. Like for instance the put( ) function takes an apple as input. Below are some example of functions.

move()
turn_left()
put('apple')
print('Hello World')

Comments

Comments are in coding so programmers can leave notes for themselves or others working on the same code. When you write a comment, the computer ignores the comment completely. In other words that line will get skipped.

To comment a single line, use a pound sign or hashtag (#). To comment multiple lines use three single quotes (”’) to start what you want to comment and three single quotes to end the block comment.

#Single-Line Comment

'''
Block Comment
Anything between the ''' is commented
'''

Indentation

Indentation in Python is very important. If you indentation is slightly off, your code will give you this error, and you won’t be able to run your code!

IndentationError: unexpected indent

If you declare or see any of these statements in your code, make sure to indent afterwards. In this class we will go over what each of these are in future lessons. For now, just know that you can indent with the TAB key.

if condition():
----INDENT
for i in range():
---INDENT
while condition():
---INDENT
def function():
---INDENT

In this class, we don’t want indentation syntax errors to be the reason why you can’t solve a problem. To help correct any of these errors, please use this tool : Online Python Formatter.

Repl.it Practice

Example 1

Example 2

Example 3


¿Qué es sintaxis?

La sintaxis es la forma en que se organiza el código para que las computadoras puedan entender lo que queremos que haga. Es como el idioma inglés, tenemos que formar oraciones de manera específica para que otras personas puedan entendernos. En el siguiente video, veremos con más detalle la sintaxis de cada uno de los elementos que se enumeran a continuación.

Las funciones

Una función es un direccion que le das a una computadora. Cada función debe incluir paréntesis al final. La razón por la cual las funciones terminan entre paréntesis es que a veces puedes poner cosas dentro de estos paréntesis para darle a la función. Como, por ejemplo, la función put () toma una manzana. A continuación se muestran algunos ejemplos de funciones.

move() #mueve
turn_left() #ir izquierda
put('apple') #poner manzana
print('Hello World') #imprimir 'Hello World'

Comentarios

Los comentarios están en el código para que los programadores puedan dejar notas para sí mismos o para otros que trabajan en el mismo código. Cuando escribe un comentario, la computadora lo ignora por completo. En otras palabras, esa línea se saltará.

Para comentar una sola línea, use un signo de almohadilla o un hashtag (#). Para comentar varias líneas, use tres comillas simples (”’) para comenzar lo que desea comentar y tres comillas simples para finalizar el comentario de bloque.

#Comentario de una linea

'''
Comentario de varias lineas
Cualquier cosa dentro de las ''' sera ignorada por la computadora
'''

Margen Adicional

El margen adicional en Python es muy importante. Si su margen adicional está disnivelada, su código le dará este error, ¡y no podrá ejecutar su código!

IndentationError: unexpected indent

Si declara o ve alguna de estas declaraciones en su código, asegúrese de aplicar un margen adicional después. En esta clase repasaremos cuáles son cada uno de estos en futuras lecciones. Por ahora, solo sepa que puede hacer un margen adicional con la tecla TAB.

if condicion():
----MARGEN ADICIONAL
for i in range():
---MARGEN ADICIONAL
while condicion():
---MARGEN ADICIONAL
def funcion():
---MARGEN ADICIONAL

En esta clase, no queremos que los errores de sintaxis de margen adicional sean la razón por la que no puede resolver un problema. Para ayudar a corregir cualquiera de estos errores, utilice esta herramienta: Online Python Formatter.

Practica de Repl.it

Ejemplo 1

Ejemplo 2

Ejemplo 3