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.

402 lines
17 KiB

3 years ago
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Python Cheat Sheet
  3. % baposter Landscape Poster
  4. % LaTeX Template
  5. % Version 1.0 (11/06/13)
  6. % baposter Class Created by:
  7. % Brian Amberg (baposter@brian-amberg.de)
  8. % This template has been downloaded from:
  9. % http://www.LaTeXTemplates.com
  10. % License:
  11. % CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
  12. % Edited by Michelle Cristina de Sousa Baltazar
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. %----------------------------------------------------------------
  15. % PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
  16. %----------------------------------------------------------------
  17. \documentclass[landscape,a0paper,fontscale=0.285]{baposter} % Adjust the font scale/size here
  18. \title{Python Cheat Sheet}
  19. \usepackage[english]{babel}
  20. \usepackage[utf8]{inputenc}
  21. \usepackage{listings} % Package for code blocks
  22. \lstset{language=python}
  23. \usepackage{graphicx} % Required for including images
  24. \graphicspath{{figures/}} % Directory in which figures are stored
  25. \usepackage{xcolor}
  26. \usepackage{colortbl}
  27. \usepackage{tabu}
  28. \usepackage{mathtools}
  29. %\usepackage{amsmath} % For typesetting math
  30. \usepackage{amssymb} % Adds new symbols to be used in math mode
  31. \usepackage{booktabs} % Top and bottom rules for tables
  32. \usepackage{enumitem} % Used to reduce itemize/enumerate spacing
  33. \usepackage{palatino} % Use the Palatino font
  34. \usepackage[font=small,labelfont=bf]{caption} % Required for specifying captions to tables and figures
  35. \usepackage{multicol} % Required for multiple columns
  36. \setlength{\columnsep}{1.5em} % Slightly increase the space between columns
  37. \setlength{\columnseprule}{0mm} % No horizontal rule between columns
  38. \usepackage{tikz} % Required for flow chart
  39. \usetikzlibrary{decorations.pathmorphing}
  40. \usetikzlibrary{shapes,arrows} % Tikz libraries required for the flow chart in the template
  41. \newcommand{\compresslist}{ % Define a command to reduce spacing within itemize/enumerate environments, this is used right after \begin{itemize} or \begin{enumerate}
  42. \setlength{\itemsep}{1pt}
  43. \setlength{\parskip}{0pt}
  44. \setlength{\parsep}{0pt}
  45. }
  46. \definecolor{lightblue}{rgb}{0.145,0.6666,1} % Defines the color used for content box headers
  47. \begin{document}
  48. \begin{poster}
  49. {
  50. headerborder=closed, % Adds a border around the header of content boxes
  51. colspacing=0.8em, % Column spacing
  52. bgColorOne=white, % Background color for the gradient on the left side of the poster
  53. bgColorTwo=white, % Background color for the gradient on the right side of the poster
  54. borderColor=violet, % Border color
  55. headerColorOne=black, % Background color for the header in the content boxes (left side)
  56. headerColorTwo=violet, % Background color for the header in the content boxes (right side)
  57. headerFontColor=white, % Text color for the header text in the content boxes
  58. boxColorOne=white, % Background color of the content boxes
  59. textborder=roundedleft, % Format of the border around content boxes, can be: none, bars, coils, triangles, rectangle, rounded, roundedsmall, roundedright or faded
  60. eyecatcher=true, % Set to false for ignoring the left logo in the title and move the title left
  61. headerheight=0.1\textheight, % Height of the header
  62. headershape=roundedright, % Specify the rounded corner in the content box headers, can be: rectangle, small-rounded, roundedright, roundedleft or rounded
  63. headerfont=\Large\bf\textsc, % Large, bold and sans serif font in the headers of content boxes
  64. %textfont={\setlength{\parindent}{1.5em}}, % Uncomment for paragraph indentation
  65. linewidth=2pt % Width of the border lines around content boxes
  66. }
  67. %----------------------------------------------------------------
  68. % Title
  69. %----------------------------------------------------------------
  70. {\bf\textsc{Python Cheat Sheet}\vspace{0.5em}} % Poster title
  71. {\textsc{ P y t h o n \ \ \ \ \ C h e a t \ \ \ \ \ S h e e t \hspace{12pt}}}
  72. {\textsc{\\ Xaloc \hspace{12pt}}}
  73. %------------------------------------------------
  74. % Python Basics
  75. %------------------------------------------------
  76. \headerbox{Python Basics:}{name=objectives,column=0,row=0}{
  77. %--------------------------------------
  78. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Hints:}}
  79. \begin{itemize}\compresslist
  80. \item Be careful with blank spaces! They can make a big difference in the code.
  81. \item Your code will not run without the correct indentation!
  82. \item \# This is a comment - use it to make a one line comment or to comment out a line
  83. \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''''''
  84. \end{itemize}
  85. %--------------------------------------
  86. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Numbers:}} \linebreak \linebreak
  87. Python uses integer and float numbers. You can use the type function to check the value of an object:\\
  88. \begin{tabular}{l l}
  89. \textbf{}\\
  90. type(3) & returns: <type 'int'> \\
  91. type(3.14) & returns: <type 'float'> \\
  92. \end{tabular}
  93. \dotfill \newline
  94. %--------------------------------------
  95. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Inputs:}}
  96. %\begin{tabular}{lp{2.0cm}lp{3.0cm}|}
  97. \begin{tabular}{lp{5.3cm}lp{3.0cm}|}
  98. A = input() & Waits for you to enter some characters and saves them in A\\
  99. \end{tabular}
  100. \begin{tabular}{lp{4.7cm}lp{3.0cm}|}
  101. B = int(input()) & Waits for you to enter integers and saves them in B \\
  102. \end{tabular}
  103. \begin{tabular}{lp{3.0cm}lp{3.0cm}|}
  104. input("Press ENTER") & Waits for you to press ENTER to continue - since there is no variable declared it won't save anything. \\
  105. \end{tabular}
  106. \begin{tabular}{lp{2.6cm}lp{3.0cm}|}
  107. A = input("message") & Prints "message" and waits for you to enter a value that will be saved in A\\
  108. \end{tabular}
  109. \vspace{0.0em} % When there are two boxes, some whitespace may need to be added if the one on the right has more content
  110. }
  111. %------------------------------------------------
  112. % Python logic
  113. %------------------------------------------------
  114. \headerbox{Basic logic in Python}{name=introduction,column=1,row=0,bottomaligned=objectives}{
  115. %------IF--------
  116. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - if}}
  117. \begin{itemize}\compresslist
  118. \item if condition1:\\
  119. \text{ }\quad ........\# do something if condition1 is true \\
  120. elif condition2:\\
  121. \text{ }\quad ........\# do something if condition2 is true \\
  122. else:\newline
  123. \text{ }\quad ........\# do something if both are false
  124. \end{itemize}
  125. %------WHILE--------
  126. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - while:}}
  127. \begin{itemize}\compresslist
  128. \item while condition:\\
  129. \text{ }\quad ........\# while condition is true keep doing something, make sure that the condition will be false at some point
  130. \end{itemize}
  131. %------FOR--------
  132. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - for:}}
  133. \begin{itemize}\compresslist
  134. \item for x in sequence\\
  135. \text{ }\quad ........\# for x in the given sequence\\
  136. \text{ }\quad ........\# do something for every item\\
  137. \text{ }\quad ........\# the sequence can be a list,\\
  138. \text{ }\quad ........\# elements from a string, etc.
  139. \item for x in range(10)\\
  140. \text{ }\quad ........\# repeat something 10 times (from 0 to 9)
  141. \item for x in range(5,10)\\
  142. \text{ }\quad ........\# repeat something 5 times (from 5 to 9)
  143. \end{itemize}
  144. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Logic tests}}
  145. \linebreak \\
  146. \begin{tabular}{l l}
  147. 10 == 10 & returns: True \\
  148. 10 == 11 & returns: False \\
  149. 10!= 11 & returns: True \\
  150. "jack" == "jack" & returns: True \\
  151. "jack" == "jake" & returns: False \\
  152. 10 > 10 & returns: False \\
  153. 10 >= 10 & returns: True \\
  154. "abc" \text{>=} "abc" & returns: True \\
  155. "abc" \text{<} "abc" & returns: False \\
  156. \end{tabular}
  157. }
  158. %------------------------------------------------
  159. % Python Lists
  160. %------------------------------------------------
  161. \headerbox{Python Lists}{name=results,column=2,span=2,row=0}{
  162. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Python Lists}}
  163. \linebreak \\
  164. \text{Lists are made form elements of any type (they can alternate types)} \linebreak \\
  165. \begin{tabular}{@{}ll@{}}
  166. \textbf{Using Lists in Python}\\
  167. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Creation} \\
  168. a\_list = [5,3,'p',9,'e'] & creates: [5,3,'p',9,'e'] \\
  169. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Accessing items} \\
  170. a\_list[0] & returns: 5 \\
  171. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Slicing} \\
  172. a\_list[1:3] & returns: [3,'p'] \\
  173. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Length} \\
  174. len(a\_list) & returns: 5 \\
  175. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}count( item)} \\
  176. \multicolumn{2}{l}{returns how many times the item was found in the list.} \\
  177. \text{ }\text{ }count(a\_list('p') & returns: 1 \\
  178. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Sorting - sort()} \\
  179. a\_list.sort() & returns: [3,5,9,'e','p'] \\
  180. \multicolumn{2}{l}{Sorting without altering the list} \\
  181. print(sorted(a\_list)) & returns: [3,5,9,'e','p'] \\
  182. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Adding - append(item)} \\
  183. a\_list.append(37) & returns: [5,3,'p',9,'e',37] \\
  184. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Inserting - insert(position,item)} \\
  185. insert(a\_list.append(3),200) & returns: [5,3,200,'p',9,'e'] \\
  186. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Retornar e remover - pop(position)} \\
  187. a\_list.pop() & returns: 'e' and the list becomes [5,3,'p',9] - deletes last element \\
  188. a\_list.pop(1) & returns: 3 and the list becomes [5,'p',9,'e'] - deletes element 1 \\
  189. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Delete - remove(item)} \\
  190. a\_list.remove('p') & returns: [5,3,9,'e'] \\
  191. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Insert} \\
  192. a\_list.insert(2,'z') & returns: [5,'z',3,'p',9,'e'] - insert in given position \\
  193. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Invert - reverse()} \\
  194. reverse(a\_list) & returns: ['e',9,'p',3,5] \\
  195. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Concatenating} \\
  196. a\_list+[0] & returns: [5,3,'p',9,'e',0] \\
  197. a\_list+a\_list & returns: [5,3,'p',9,'e',5,3,'p',9,'e'] \\
  198. \multicolumn{2}{l}{\cellcolor[HTML]{ECB6F9}Find} \\
  199. 9 in a\_list & returns: True \\
  200. for x in a\_list & returns the whole list, one element per line \\
  201. \text{ }\quad ......print(x) &
  202. \end{tabular}
  203. %------------------------------------------------
  204. }
  205. \end{poster}
  206. \newpage
  207. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  208. %%%%%%%%%%%%%%%%%% SECOND PAGE %%%%%%%%%%%%%%%%%%
  209. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  210. \begin{poster}
  211. {
  212. headerborder=closed, colspacing=0.8em, bgColorOne=white, bgColorTwo=white, borderColor=violet, headerColorOne=black, headerColorTwo=violet,
  213. headerFontColor=white, boxColorOne=white, textborder=roundedleft, eyecatcher=true, headerheight=0.1\textheight, headershape=roundedright, headerfont=\Large\bf\textsc, linewidth=2pt
  214. }
  215. %----------------------------------------------------------------
  216. % TITLE SECTION
  217. %----------------------------------------------------------------
  218. {\bf\textsc{Python Cheat Sheet}\vspace{0.5em}} % Poster title
  219. {\textsc{ P y t h o n \ \ \ \ \ C h e a t \ \ \ \ \ S h e e t \hspace{12pt}}}
  220. {\textsc{\\ Xaloc \hspace{12pt}}}
  221. %----------------------------------------------------------------
  222. % Other Elements
  223. %----------------------------------------------------------------
  224. \headerbox{Other Elements}{name=method,column=0}{
  225. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Key words}}
  226. \begin{tabular}{lp{5.8cm}lp{1.0cm}}
  227. {\bf Oper.} & {\bf Description}\\
  228. print & prints on the screen \\
  229. break & stops a loop if necessary\\
  230. continue & restarts loop ignoring commands below \\
  231. is & Tries an object identity \\
  232. def & Used to create a new function defined by the user \\
  233. return & Exit the function and returns a value \\
  234. global & Access variables defined globally (outside of a function) \\
  235. del & Deletes objects \\
  236. \end{tabular}
  237. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Libraries}}
  238. Libraries are a collection of functions and methods that allow you to perform many actions without writing your code\linebreak \\
  239. \textbf{Using Libraries in Python}\\
  240. \begin{tabular}{lp{5.8cm}lp{1.0cm}}
  241. import & imports a library inside a script\\
  242. as & gives an alias to the library \\
  243. from & imports a specific function from a library\\
  244. \end{tabular}
  245. \linebreak
  246. \textbf{Useful Libraries}\\
  247. \begin{tabular}{lp{5.8cm}lp{1.0cm}}
  248. numpy & library for maths\\
  249. matplotlib & library for plotting \\
  250. tkinter & library to create GUI\\
  251. random & library for random numbers\\
  252. \end{tabular}
  253. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Functions}}
  254. \begin{itemize}\compresslist
  255. \item def f\_name(args):\\
  256. \text{ }\quad ........\# operations done by the function
  257. \text{ }\quad return value\_to\_return
  258. \end{itemize}
  259. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Dictionaries}}
  260. A dictionary is a list of keys and values, where you can access a value by its key. All keys must be different.\\
  261. car = ["brand" : "Hummer",\\
  262. \text{ }\quad\text{ }\quad "model":"H2",\\
  263. \text{ }\quad\text{ }\quad "year":2009,]\\
  264. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Tuples}}
  265. 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):\\
  266. a\_tuple = ('a','b','c')
  267. }
  268. %----------------------------------------------------------------
  269. % Operators
  270. %----------------------------------------------------------------
  271. \headerbox{Python Operators}{name=results2,column=1}{
  272. Lets take a=10 and b=20 as example:\\
  273. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Arithmetic operators}}
  274. \begin{tabular}{lll}
  275. {\bf Op.} & {\bf Description} & {\bf Example} \\
  276. + & Addition & a + b returns: 30 \\
  277. - & Subtraction & a - b returns: -10 \\
  278. * & Multiplication& a * b returns: 200 \\
  279. / & Division & b / a returns: 2 \\
  280. \% & Module & a \% b returns: 0 \\
  281. ** & Exponential & a**b returns: $10^{20}$ \\
  282. // & Euclidean Division & 9 // 2 returns: 4
  283. \end{tabular}
  284. %----Comparison operators-----------
  285. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Comparison Operators}}
  286. 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.\\
  287. \begin{tabular}{lll}
  288. {\bf Op.} & {\bf Description} & {\bf Example} \\
  289. < & Less than & a < b returns: True \\
  290. <= & Less or equal & a <= b returns: True \\
  291. == & Equal & a == b returns: False \\
  292. > & Greater than & a > b returns: False \\
  293. >= & Greater or equal & a >= b returns: False \\
  294. != & Different & a != b returns: True
  295. \end{tabular}
  296. %------Logic Operators-----------
  297. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - Logic Operators}}
  298. The logic operators {\bf and} and {\bf or} Also return a Boolean value when used in a decision structure.\\
  299. \begin{tabular}{lp{6.5cm}lp{1.0cm}|}
  300. {\bf Op.} & {\bf Description}\\
  301. and & If the result of both sides is true, returns: True \\
  302. or & If one of the results on either side is true, returns: True \\
  303. not & It is used to invert the result of any Boolean operation.
  304. \end{tabular}
  305. %------Operations with strings----------
  306. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf - String Operators}}
  307. Using a=['Hello'] and b=['Python']
  308. \begin{tabular}{lp{6.5cm}lp{1.0cm}lp{1.0cm}} %\begin{tabular}{lll}
  309. {\bf Oper.} & {\bf Example} \\
  310. + & a + b returns: HelloPython \\
  311. * & a*2 returns: HelloHello \\
  312. .[ ] & a[1] returns: "e" \\
  313. .[ : ] & a[1:4] returns: "ell" \\
  314. in & H in a will give 1 \\
  315. not in & M not in a returns: 1 \\
  316. \end{tabular}
  317. }
  318. %----------------------------------------------------------------
  319. % Examples
  320. %----------------------------------------------------------------
  321. \headerbox{Examples}{name=conclusion,column=2,span=2,row=0}{
  322. A few examples to see how actual python code looks like.
  323. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Simple Hello World with print}}
  324. \lstinputlisting{simple_hello.py}
  325. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Hello World with function}}
  326. \lstinputlisting{func_hello.py}
  327. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Hello your name}}
  328. \lstinputlisting{hello_name.py}
  329. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf Using a library}}
  330. \lstinputlisting{library.py}
  331. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf if loop}}
  332. \lstinputlisting{if.py}
  333. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf while loop}}
  334. \lstinputlisting{while.py}
  335. \colorbox[HTML]{DA74F3}{\makebox[\textwidth-2\fboxsep][l]{\bf for loop}}
  336. \lstinputlisting{for.py}
  337. }
  338. \end{poster}
  339. \end{document}