Explicación de los elementos básicos de python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

403 lines
17 KiB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Python Cheat Sheet
% baposter Landscape Poster
% LaTeX Template
% Version 1.0 (11/06/13)
% baposter Class Created by:
% Brian Amberg (baposter@brian-amberg.de)
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
% Edited by Michelle Cristina de Sousa Baltazar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------
\documentclass[landscape,a0paper,fontscale=0.285]{baposter} % Adjust the font scale/size here
\title{Python Cheat Sheet}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{listings} % Package for code blocks
\lstset{language=python}
\usepackage{graphicx} % Required for including images
\graphicspath{{figures/}} % Directory in which figures are stored
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{tabu}
\usepackage{mathtools}
%\usepackage{amsmath} % For typesetting math
\usepackage{amssymb} % Adds new symbols to be used in math mode
\usepackage{booktabs} % Top and bottom rules for tables
\usepackage{enumitem} % Used to reduce itemize/enumerate spacing
\usepackage{palatino} % Use the Palatino font
\usepackage[font=small,labelfont=bf]{caption} % Required for specifying captions to tables and figures
\usepackage{multicol} % Required for multiple columns
\setlength{\columnsep}{1.5em} % Slightly increase the space between columns
\setlength{\columnseprule}{0mm} % No horizontal rule between columns
\usepackage{tikz} % Required for flow chart
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{shapes,arrows} % Tikz libraries required for the flow chart in the template
\newcommand{\compresslist}{ % Define a command to reduce spacing within itemize/enumerate environments, this is used right after \begin{itemize} or \begin{enumerate}
\setlength{\itemsep}{1pt}
\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
}
\definecolor{lightblue}{rgb}{0.145,0.6666,1} % Defines the color used for content box headers
\begin{document}
\begin{poster}
{
headerborder=closed, % Adds a border around the header of content boxes
colspacing=0.8em, % Column spacing
bgColorOne=white, % Background color for the gradient on the left side of the poster
bgColorTwo=white, % Background color for the gradient on the right side of the poster
borderColor=violet, % Border color
headerColorOne=black, % Background color for the header in the content boxes (left side)
headerColorTwo=violet, % Background color for the header in the content boxes (right side)
headerFontColor=white, % Text color for the header text in the content boxes
boxColorOne=white, % Background color of the content boxes
textborder=roundedleft, % Format of the border around content boxes, can be: none, bars, coils, triangles, rectangle, rounded, roundedsmall, roundedright or faded
eyecatcher=true, % Set to false for ignoring the left logo in the title and move the title left
headerheight=0.1\textheight, % Height of the header
headershape=roundedright, % Specify the rounded corner in the content box headers, can be: rectangle, small-rounded, roundedright, roundedleft or rounded
headerfont=\Large\bf\textsc, % Large, bold and sans serif font in the headers of content boxes
%textfont={\setlength{\parindent}{1.5em}}, % Uncomment for paragraph indentation
linewidth=2pt % Width of the border lines around content boxes
}
%----------------------------------------------------------------
% Title
%----------------------------------------------------------------
{\bf\textsc{Python Cheat Sheet}\vspace{0.5em}} % Poster title
{\textsc{ P y t h o n \ \ \ \ \ C h e a t \ \ \ \ \ S h e e t \hspace{12pt}}}
{\textsc{\\ Xaloc \hspace{12pt}}}
%------------------------------------------------
% Python Basics
%------------------------------------------------
\headerbox{Python Basics:}{name=objectives,column=0,row=0}{
%--------------------------------------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Hints:}}
\begin{itemize}\compresslist
\item Be careful with blank spaces! They can make a big difference in the code.
\item Your code will not run without the correct indentation!
\item \# This is a comment - use it to make a one line comment or to comment out a line
\item ''''''\newline Everything in between three quote marks will be considered a comment - it can be used to make comments that span more than one line with line breaks in them \newline''''''
\end{itemize}
%--------------------------------------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Numbers:}} \linebreak \linebreak
Python uses integer and float numbers. You can use the type function to check the value of an object:\\
\begin{tabular}{l l}
\textbf{}\\
type(3) & returns: <type 'int'> \\
type(3.14) & returns: <type 'float'> \\
\end{tabular}
\dotfill \newline
%--------------------------------------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Inputs:}}
%\begin{tabular}{lp{2.0cm}lp{3.0cm}|}
\begin{tabular}{lp{5.3cm}lp{3.0cm}|}
A = input() & Waits for you to enter some characters and saves them in A\\
\end{tabular}
\begin{tabular}{lp{4.7cm}lp{3.0cm}|}
B = int(input()) & Waits for you to enter integers and saves them in B \\
\end{tabular}
\begin{tabular}{lp{3.0cm}lp{3.0cm}|}
input("Press ENTER") & Waits for you to press ENTER to continue - since there is no variable declared it won't save anything. \\
\end{tabular}
\begin{tabular}{lp{2.6cm}lp{3.0cm}|}
A = input("message") & Prints "message" and waits for you to enter a value that will be saved in A\\
\end{tabular}
\vspace{0.0em} % When there are two boxes, some whitespace may need to be added if the one on the right has more content
}
%------------------------------------------------
% Python logic
%------------------------------------------------
\headerbox{Basic logic in Python}{name=introduction,column=1,row=0,bottomaligned=objectives}{
%------IF--------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - if}}
\begin{itemize}\compresslist
\item if condition1:\\
\text{ }\quad ........\# do something if condition1 is true \\
elif condition2:\\
\text{ }\quad ........\# do something if condition2 is true \\
else:\newline
\text{ }\quad ........\# do something if both are false
\end{itemize}
%------WHILE--------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - while:}}
\begin{itemize}\compresslist
\item while condition:\\
\text{ }\quad ........\# while condition is true keep doing something, make sure that the condition will be false at some point
\end{itemize}
%------FOR--------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - for:}}
\begin{itemize}\compresslist
\item for x in sequence\\
\text{ }\quad ........\# for x in the given sequence\\
\text{ }\quad ........\# do something for every item\\
\text{ }\quad ........\# the sequence can be a list,\\
\text{ }\quad ........\# elements from a string, etc.
\item for x in range(10)\\
\text{ }\quad ........\# repeat something 10 times (from 0 to 9)
\item for x in range(5,10)\\
\text{ }\quad ........\# repeat something 5 times (from 5 to 9)
\end{itemize}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Logic tests}}
\linebreak \\
\begin{tabular}{l l}
10 == 10 & returns: True \\
10 == 11 & returns: False \\
10!= 11 & returns: True \\
"jack" == "jack" & returns: True \\
"jack" == "jake" & returns: False \\
10 > 10 & returns: False \\
10 >= 10 & returns: True \\
"abc" \text{>=} "abc" & returns: True \\
"abc" \text{<} "abc" & returns: False \\
\end{tabular}
}
%------------------------------------------------
% Python Lists
%------------------------------------------------
\headerbox{Python Lists}{name=results,column=2,span=2,row=0}{
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Python Lists}}
\linebreak \\
\text{Lists are made form elements of any type (they can alternate types)} \linebreak \\
\begin{tabular}{@{}ll@{}}
\textbf{Using Lists in Python}\\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Creation} \\
a\_list = [5,3,'p',9,'e'] & creates: [5,3,'p',9,'e'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Accessing items} \\
a\_list[0] & returns: 5 \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Slicing} \\
a\_list[1:3] & returns: [3,'p'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Length} \\
len(a\_list) & returns: 5 \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}count( item)} \\
\multicolumn{2}{l}{returns how many times the item was found in the list.} \\
\text{ }\text{ }count(a\_list('p') & returns: 1 \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Sorting - sort()} \\
a\_list.sort() & returns: [3,5,9,'e','p'] \\
\multicolumn{2}{l}{Sorting without altering the list} \\
print(sorted(a\_list)) & returns: [3,5,9,'e','p'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Adding - append(item)} \\
a\_list.append(37) & returns: [5,3,'p',9,'e',37] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Inserting - insert(position,item)} \\
insert(a\_list.append(3),200) & returns: [5,3,200,'p',9,'e'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Retornar e remover - pop(position)} \\
a\_list.pop() & returns: 'e' and the list becomes [5,3,'p',9] - deletes last element \\
a\_list.pop(1) & returns: 3 and the list becomes [5,'p',9,'e'] - deletes element 1 \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Delete - remove(item)} \\
a\_list.remove('p') & returns: [5,3,9,'e'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Insert} \\
a\_list.insert(2,'z') & returns: [5,'z',3,'p',9,'e'] - insert in given position \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Invert - reverse()} \\
reverse(a\_list) & returns: ['e',9,'p',3,5] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Concatenating} \\
a\_list+[0] & returns: [5,3,'p',9,'e',0] \\
a\_list+a\_list & returns: [5,3,'p',9,'e',5,3,'p',9,'e'] \\
\multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Find} \\
9 in a\_list & returns: True \\
for x in a\_list & returns the whole list, one element per line \\
\text{ }\quad ......print(x) &
\end{tabular}
%------------------------------------------------
}
\end{poster}
\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% SECOND PAGE %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{poster}
{
headerborder=closed, colspacing=0.8em, bgColorOne=white, bgColorTwo=white, borderColor=violet, headerColorOne=black, headerColorTwo=violet,
headerFontColor=white, boxColorOne=white, textborder=roundedleft, eyecatcher=true, headerheight=0.1\textheight, headershape=roundedright, headerfont=\Large\bf\textsc, linewidth=2pt
}
%----------------------------------------------------------------
% TITLE SECTION
%----------------------------------------------------------------
{\bf\textsc{Python Cheat Sheet}\vspace{0.5em}} % Poster title
{\textsc{ P y t h o n \ \ \ \ \ C h e a t \ \ \ \ \ S h e e t \hspace{12pt}}}
{\textsc{\\ Xaloc \hspace{12pt}}}
%----------------------------------------------------------------
% Other Elements
%----------------------------------------------------------------
\headerbox{Other Elements}{name=method,column=0}{
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Key words}}
\begin{tabular}{lp{5.8cm}lp{1.0cm}}
{\bf Oper.} & {\bf Description}\\
print & prints on the screen \\
break & stops a loop if necessary\\
continue & restarts loop ignoring commands below \\
is & Tries an object identity \\
def & Used to create a new function defined by the user \\
return & Exit the function and returns a value \\
global & Access variables defined globally (outside of a function) \\
del & Deletes objects \\
\end{tabular}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Libraries}}
Libraries are a collection of functions and methods that allow you to perform many actions without writing your code\linebreak \\
\textbf{Using Libraries in Python}\\
\begin{tabular}{lp{5.8cm}lp{1.0cm}}
import & imports a library inside a script\\
as & gives an alias to the library \\
from & imports a specific function from a library\\
\end{tabular}
\linebreak
\textbf{Useful Libraries}\\
\begin{tabular}{lp{5.8cm}lp{1.0cm}}
numpy & library for maths\\
matplotlib & library for plotting \\
tkinter & library to create GUI\\
random & library for random numbers\\
\end{tabular}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Functions}}
\begin{itemize}\compresslist
\item def f\_name(args):\\
\text{ }\quad ........\# operations done by the function
\text{ }\quad return value\_to\_return
\end{itemize}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Dictionaries}}
A dictionary is a list of keys and values, where you can access a value by its key. All keys must be different.\\
car = ["brand" : "Hummer",\\
\text{ }\quad\text{ }\quad "model":"H2",\\
\text{ }\quad\text{ }\quad "year":2009,]\\
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Tuples}}
A tuple is a list of values separated by a comma - very similar to a list but tuples are immutable (you are not allowed to change their values):\\
a\_tuple = ('a','b','c')
}
%----------------------------------------------------------------
% Operators
%----------------------------------------------------------------
\headerbox{Python Operators}{name=results2,column=1}{
Lets take a=10 and b=20 as example:\\
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Arithmetic operators}}
\begin{tabular}{lll}
{\bf Op.} & {\bf Description} & {\bf Example} \\
+ & Addition & a + b returns: 30 \\
- & Subtraction & a - b returns: -10 \\
* & Multiplication& a * b returns: 200 \\
/ & Division & b / a returns: 2 \\
\% & Module & a \% b returns: 0 \\
** & Exponential & a**b returns: $10^{20}$ \\
// & Euclidean Division & 9 // 2 returns: 4
\end{tabular}
%----Comparison operators-----------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Comparison Operators}}
Basic comparation opration can be used in different ways for any type of value - numbers, strings, sequences, lists, etc. The answer will always be True or False.\\
\begin{tabular}{lll}
{\bf Op.} & {\bf Description} & {\bf Example} \\
< & Less than & a < b returns: True \\
<= & Less or equal & a <= b returns: True \\
== & Equal & a == b returns: False \\
> & Greater than & a > b returns: False \\
>= & Greater or equal & a >= b returns: False \\
!= & Different & a != b returns: True
\end{tabular}
%------Logic Operators-----------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Logic Operators}}
The logic operators {\bf and} and {\bf or} Also return a Boolean value when used in a decision structure.\\
\begin{tabular}{lp{6.5cm}lp{1.0cm}|}
{\bf Op.} & {\bf Description}\\
and & If the result of both sides is true, returns: True \\
or & If one of the results on either side is true, returns: True \\
not & It is used to invert the result of any Boolean operation.
\end{tabular}
%------Operations with strings----------
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - String Operators}}
Using a=['Hello'] and b=['Python']
\begin{tabular}{lp{6.5cm}lp{1.0cm}lp{1.0cm}} %\begin{tabular}{lll}
{\bf Oper.} & {\bf Example} \\
+ & a + b returns: HelloPython \\
* & a*2 returns: HelloHello \\
.[ ] & a[1] returns: "e" \\
.[ : ] & a[1:4] returns: "ell" \\
in & H in a will give 1 \\
not in & M not in a returns: 1 \\
\end{tabular}
}
%----------------------------------------------------------------
% Examples
%----------------------------------------------------------------
\headerbox{Examples}{name=conclusion,column=2,span=2,row=0}{
A few examples to see how actual python code looks like.
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Simple Hello World with print}}
\lstinputlisting{simple_hello.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Hello World with function}}
\lstinputlisting{func_hello.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Hello your name}}
\lstinputlisting{hello_name.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Using a library}}
\lstinputlisting{library.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf if loop}}
\lstinputlisting{if.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf while loop}}
\lstinputlisting{while.py}
\colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf for loop}}
\lstinputlisting{for.py}
}
\end{poster}
\end{document}