├── .gitattributes ├── Python_for_Java_developers_cheat_sheet.pdf ├── README.md ├── python-for-coding-interviews.pdf ├── resources └── images │ ├── python_for_cp_1.png │ ├── python_for_cp_2.png │ ├── python_for_java_1.png │ ├── python_for_java_2.png │ ├── python_for_java_3.png │ └── python_for_java_4.png └── source └── python_for_java_developers.tex /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /Python_for_Java_developers_cheat_sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/Python_for_Java_developers_cheat_sheet.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Awesome Python CheatSheets 2 | #### This repo is created with the aim of providing python cheatsheets for developers that help them in study and in work. 3 | ##### Note: if you like my work, please support by adding yourself for [LessenText](https://yep.so/p/lessentextai?ref=github) Beta program. 4 | --- 5 | Table of contents 6 | ================= 7 | 8 | 9 | ### 1. [Python for Java Developers](#1-python-for-java-developers-1) 10 | ### 2. [Python for Competitive Programmers](#2-python-for-coding-interviews) 11 | 12 | Contents 13 | ======== 14 | ## 1. Python for Java Developers 15 | 16 | #### If you are a Java developer Or just know the basics of programming and want to learn Python, this cheatsheet is perfect for you. 17 | [View the PDF version](https://github.com/akashp1712/awesome-python-cheatsheets/blob/master/Python_for_Java_developers_cheat_sheet.pdf) / [Find it on Medium](https://medium.com/nestedif/cheatsheet-python-for-java-developers-98f75c94a1a) 18 | 19 |
20 |
21 |
22 | 23 |
24 |
25 |
26 | 27 |
28 |
29 |
30 | 31 |
32 |
33 |
34 | 35 | --- 36 | 37 | 38 | ## 2. Python for Coding Interviews 39 | #### Useful tricks and tips for Coding Interviews 40 | [View the PDF version](https://github.com/akashp1712/awesome-python-cheatsheets/blob/master/python-for-coding-interviews.pdf) 41 | 42 |
43 |
44 |
45 | 46 |
47 |
48 |
49 | 50 | --- 51 | # Credits 52 | 53 | The Latex Template used or this cheat-sheet is provided by the Karol Kozioł (www.karol-koziol.net) for ShareLaTeX. 54 | 55 | The cheat sheet is in Latex format and needs to be compiled to be converted in PDF doc. 56 | -------------------------------------------------------------------------------- /python-for-coding-interviews.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/python-for-coding-interviews.pdf -------------------------------------------------------------------------------- /resources/images/python_for_cp_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_cp_1.png -------------------------------------------------------------------------------- /resources/images/python_for_cp_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_cp_2.png -------------------------------------------------------------------------------- /resources/images/python_for_java_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_java_1.png -------------------------------------------------------------------------------- /resources/images/python_for_java_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_java_2.png -------------------------------------------------------------------------------- /resources/images/python_for_java_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_java_3.png -------------------------------------------------------------------------------- /resources/images/python_for_java_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashp1712/awesome-python-cheatsheets/b0810b25b966453039da61918280a5dbd51a6dd9/resources/images/python_for_java_4.png -------------------------------------------------------------------------------- /source/python_for_java_developers.tex: -------------------------------------------------------------------------------- 1 | % Template created by Karol Kozioł (www.karol-koziol.net) for ShareLaTeX 2 | 3 | \documentclass[a4paper,9pt]{extarticle} 4 | \usepackage[utf8]{inputenc} 5 | \usepackage[T1]{fontenc} 6 | \usepackage{graphicx} 7 | \usepackage{xcolor} 8 | \usepackage{tikz} 9 | \usepackage{fontawesome} 10 | \usepackage{hyperref} 11 | \hypersetup{ 12 | colorlinks=true, 13 | linkcolor=blue, 14 | filecolor=magenta, 15 | urlcolor=blue, 16 | } 17 | 18 | \urlstyle{same} 19 | 20 | \usepackage{amsmath,amssymb,textcomp} 21 | \everymath{\displaystyle} 22 | 23 | \usepackage{times} 24 | \renewcommand\familydefault{\sfdefault} 25 | \usepackage{tgheros} 26 | \usepackage[defaultmono,scale=0.85]{droidmono} 27 | 28 | \usepackage{multicol} 29 | \setlength{\columnseprule}{0pt} 30 | \setlength{\columnsep}{20.0pt} 31 | 32 | % Style for coding: Starts % 33 | \usepackage{listings} 34 | \usepackage{color} 35 | 36 | \definecolor{dkgreen}{rgb}{0,0.6,0} 37 | \definecolor{gray}{rgb}{0.5,0.5,0.5} 38 | \definecolor{mauve}{rgb}{0.58,0,0.82} 39 | 40 | \lstset{frame=tb, 41 | language=Python, 42 | aboveskip=3mm, 43 | belowskip=3mm, 44 | showstringspaces=false, 45 | columns=flexible, 46 | basicstyle={\small\ttfamily}, 47 | numbers=none, 48 | numberstyle=\tiny\color{gray}, 49 | keywordstyle=\color{blue}, 50 | commentstyle=\color{dkgreen}, 51 | stringstyle=\color{mauve}, 52 | breaklines=true, 53 | breakatwhitespace=true, 54 | tabsize=3 55 | } 56 | 57 | % Style for coding: Ends % 58 | 59 | \usepackage{geometry} 60 | \geometry{ 61 | a4paper, 62 | total={210mm,297mm}, 63 | left=10mm,right=10mm,top=10mm,bottom=15mm} 64 | 65 | \linespread{1.3} 66 | 67 | % custom title 68 | \makeatletter 69 | \renewcommand*{\maketitle}{% 70 | \noindent 71 | \begin{minipage}{0.65\textwidth} 72 | \begin{tikzpicture} 73 | \node[rectangle,rounded corners=6pt,inner sep=10pt,fill=blue!50!black,text width= 0.95\textwidth] {\color{white}\Huge \@title}; 74 | \end{tikzpicture} 75 | \end{minipage} 76 | \hfill 77 | \begin{minipage}{0.25\textwidth} 78 | \begin{tikzpicture} 79 | \node[rectangle,rounded corners=3pt,inner sep=10pt,draw=blue!50!black,text width= 0.95\textwidth] {\LARGE Referring python 3.7.1}; 80 | \end{tikzpicture} 81 | \end{minipage} 82 | \bigskip\bigskip 83 | }% 84 | \makeatother 85 | 86 | % custom section 87 | \usepackage[explicit]{titlesec} 88 | \newcommand*\sectionlabel{} 89 | \titleformat{\section} 90 | {\gdef\sectionlabel{} 91 | \normalfont\sffamily\Large\bfseries\scshape} 92 | {\gdef\sectionlabel{\thesection\ }}{0pt} 93 | { 94 | \noindent 95 | \begin{tikzpicture} 96 | \node[rectangle,rounded corners=3pt,inner sep=4pt,fill=blue!50!black,text width= 0.95\columnwidth] {\color{white}\sectionlabel#1}; 97 | \end{tikzpicture} 98 | } 99 | \titlespacing*{\section}{0pt}{15pt}{10pt} 100 | 101 | 102 | % custom footer 103 | \usepackage{fancyhdr} 104 | \makeatletter 105 | \pagestyle{fancy} 106 | \fancyhead{} 107 | \fancyfoot[C]{\footnotesize \textcopyright\ \@date\ \ \@author} 108 | \renewcommand{\headrulewidth}{0pt} 109 | \renewcommand{\footrulewidth}{0pt} 110 | \makeatother 111 | 112 | \title{Python for JAVA Developers: Basics V 1.2} 113 | \author{Akash Panchal - www.medium.com/@akashp1712} 114 | \date{2019} 115 | 116 | \begin{document} 117 | 118 | \maketitle 119 | \begin{multicols*}{2} 120 | 121 | \section{Basic Syntax} 122 | \subsection{End of Statements} 123 | Unlike the Java, to end a statement in Python, we don't have to type in a semicolon, you simply press \boxed{Enter}. But semicolons can be used to delimit statements if you wish to put multiple statements on the same line. 124 | \begin{lstlisting} 125 | message1 = 'Hello World!' 126 | message2 = "Python gives no missing semicolon error!" 127 | 128 | # Instead of System.out.print, we use print 129 | print (message1) # print 'Hello World!' on the console output 130 | print ("Hello"); print ("Python!"); # usage of the semicolon 131 | 132 | \end{lstlisting} 133 | 134 | \subsection{Code Blocks and Indentation} 135 | One of the most distinctive features of Python is its use of indentation to mark blocks of code. 136 | Consider the following if-statement from our non-zero number checking program: 137 | 138 | \textbf{JAVA} 139 | \begin{lstlisting} 140 | if (0 == value) { 141 | System.out.print("Number is Zero"); 142 | } else { 143 | System.out.print("Number is non-Zero."); 144 | } 145 | 146 | System.out.print("All done!"); 147 | \end{lstlisting} 148 | 149 | \textbf{Python} 150 | \begin{lstlisting} 151 | if 0 == value: 152 | print('Number is Zero') 153 | else: 154 | print('Number is non-Zero.') 155 | 156 | print('All done!') 157 | \end{lstlisting} 158 | 159 | To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented \textbf{four spaces}, which is a typical amount of indentation for Python. 160 | 161 | \section{Variables} 162 | 163 | \subsection{Declaration} 164 | Variables are created the first time a value is assigned to them. There is no concept of the declaration of the data type in python. 165 | \begin{lstlisting} 166 | number = 11 167 | string = "This is a string" 168 | \end{lstlisting} 169 | You declare multiple variables by separating each variable name with a comma. 170 | \begin{lstlisting} 171 | a, b = True, False 172 | \end{lstlisting} 173 | 174 | \subsection{Assigning Values} 175 | \begin{lstlisting} 176 | a = 300 177 | \end{lstlisting} 178 | The same value can be assigned to multiple variables at the same time: 179 | \begin{lstlisting} 180 | a = b = c = 1 181 | \end{lstlisting} 182 | And multiple variables can be assigned different values on a single line: 183 | \begin{lstlisting} 184 | a, b, c = 1, 2, "john" 185 | \end{lstlisting} 186 | This is the same as: 187 | \begin{lstlisting} 188 | a = 1 189 | b = 2 190 | c = "john" 191 | \end{lstlisting} 192 | 193 | \section{Data Types} 194 | Python sets the variable type based on the value that is assigned to it. Unlike JAVA, Python will change the variable type if the variable value is set to another value. 195 | 196 | \begin{lstlisting} 197 | var = 123 # This will create a number integer assignment 198 | var = 'john' # the `var` variable is now a string type. 199 | \end{lstlisting} 200 | 201 | \subsection{Numbers} 202 | Most of the time using the standard Python number type is fine. Python will automatically convert a number from one type to another whenever required. We don't require to use the type casting like JAVA. 203 | 204 | \begin{center} 205 | \begin{tabular}{||c | c | c | c||} 206 | \hline 207 | Type & Java & Python & Description \\ [0.5ex] 208 | \hline\hline 209 | int & int a = 11 & a = 11 & Signed Integer\\ 210 | \hline 211 | long & long a = 1712L & a = 1712L & (L) Long integers\\ 212 | \hline 213 | float & float a = 19.91 & a = 19.91 & (.) Floating point values\\ 214 | \hline 215 | complex & - - - & a = 3.14J & (J) integer [0 to 255]\\ 216 | \hline 217 | \end{tabular} 218 | \end{center} 219 | 220 | \newpage 221 | \subsection{String} 222 | Create string variables by enclosing characters in quotes. Python uses single quotes \boxed{'} double quotes \boxed{"} and triple quotes \boxed{"""} to denote literal strings. Only the triple quoted strings \boxed{"""} will automatically continue across the end of line statement. 223 | 224 | \begin{lstlisting} 225 | firstName = 'Jane' 226 | lastName = "Doe" 227 | message = """This is a string that will span across multiple lines. Using newline characters and no spaces for the next lines.""" 228 | \end{lstlisting} 229 | 230 | Key Methods: 231 | \begin{center} 232 | \begin{tabular}{||c | c||} 233 | \hline 234 | Java & Python\\ [1ex] 235 | \hline\hline 236 | charAt() & find()\\ 237 | \hline 238 | indexOf() & index()\\ 239 | \hline 240 | length() & len()\\ 241 | \hline 242 | replace() & replace()\\ 243 | \hline 244 | toString() & str()\\ 245 | \hline 246 | trim() & rstrip(), lstrip()\\ 247 | \hline 248 | \end{tabular} 249 | \end{center} 250 | 251 | Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings. 252 | 253 | \subsection{Boolean} 254 | Python provides the boolean type that can be either set to False or True. 255 | In python, every object has a boolean value. The following elements are false: 256 | \begin{itemize} 257 | \item[$\bullet$] None 258 | \item[$\bullet$] False 259 | \item[$\bullet$] 0 260 | \item[$\bullet$] Empty collections: “”, (), [], {} 261 | \end{itemize} 262 | 263 | All other objects are True. 264 | \textbf{Note} that, In JAVA \textbf{null} is not false while in python None is. 265 | 266 | \subsection{List} 267 | The List is one of the most powerful variable type (data structure!) in Python. A list can contain a series of values. Unlike JAVA, the list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists are mutable. 268 | 269 | List variables are declared by using brackets \boxed{[ ]} following the variable name. 270 | 271 | \begin{lstlisting} 272 | A = [] # This is a blank list variable. 273 | B = [1, 23, 45, 67] # creates an initial list of 4 numbers. 274 | C = [2, 4, 'john'] # can contain different variable types. 275 | D = ["A", B, C] # can contains other list objects as well. 276 | \end{lstlisting} 277 | 278 | Key Methods: 279 | \begin{center} 280 | \begin{tabular}{||c | c||} 281 | \hline 282 | Java ArrayList & Python list\\ [1ex] 283 | \hline\hline 284 | list = new ArrayList() & list=[]\\ 285 | \hline 286 | list.add(object) & list.append(object)\\ 287 | \hline 288 | list.get(i) & list[i]\\ 289 | \hline 290 | list.size() & len(list)\\ 291 | \hline 292 | list2= list.clone() & list2=list[:]\\ 293 | \hline 294 | \end{tabular} 295 | \end{center} 296 | 297 | \subsection{Tuple} 298 | A tuple is another useful variable type similar to list and can contain heterogeneous values. Unlike the list, a tuple is \textbf{immutable} And is like a static array. 299 | 300 | A tuple is \textbf{fixed in size} and that is why tuples are replacing array completely as they are more efficient in all parameters. 301 | 302 | Tuple can be used as an alternative of \textbf{list(python/Java)} OR \textbf{Array(Java)} with respect to the use cases. i.e, If you have a dataset which will be assigned only once and its value should not change again, you need a tuple. 303 | 304 | Tuple variables are declared by using parentheses \boxed{( )} following the variable name. 305 | 306 | \begin{lstlisting} 307 | A = () # This is a blank tuple variable. 308 | B = (1, 23, 45, 67) # creates a tuple of 4 numbers. 309 | C = (2, 4, 'john') # can contain different variable types. 310 | D = ("A", B, C) # can contains other tuple objects as well. 311 | \end{lstlisting} 312 | 313 | \subsection{Dictionary} 314 | Dictionaries in Python are lists of \textbf{Key: Value} pairs. This is a very powerful datatype to hold a lot of related information that can be associated through keys. 315 | Dictionary in Python can be the Alternative of the Map in JAVA. But again a Dictionary in python can contain \textbf{heterogeneous} key as well as value. 316 | 317 | \begin{lstlisting} 318 | room_num = {'john': 121, 'tom': 307} 319 | room_num['john'] = 432 # set the value associated with the 'john' key to 432 320 | print (room_num['tom']) # print the value of the 'tom' key. 321 | room_num['isaac'] = 345 # Add a new key 'isaac' with the associated value 322 | print (room_num.keys()) # print out a list of keys in the dictionary 323 | print ('isaac' in room_num) # test to see if 'issac' is in the dictionary. This returns true. 324 | 325 | hotel_name = {1: "Taj", "two": "Woodland", "next": 3.14} # this is totally valid in python 326 | \end{lstlisting} 327 | 328 | Key Methods: 329 | \begin{center} 330 | \begin{tabular}{||c | c||} 331 | \hline 332 | Java HashMap & Python Dictionary\\ [1ex] 333 | \hline\hline 334 | Map myMap = new HashMap() & my\_dict = \{ \}\\ 335 | \hline 336 | clear() & clear()\\ 337 | \hline 338 | clone() & copy()\\ 339 | \hline 340 | containsKey(key) & key \textbf{in} my\_dict\\ 341 | \hline 342 | get(key) & get(key)\\ 343 | \hline 344 | keySet() & keys()\\ 345 | \hline 346 | put(key, value) & my\_dict[key] = value\\ 347 | \hline 348 | remove(key) & pop(key)\\ 349 | \hline 350 | size() & len(my\_dict)\\ 351 | \hline 352 | \end{tabular} 353 | \end{center} 354 | 355 | \section{Operators} 356 | \subsubsection{Operator Precedence} 357 | Operator Precedence is same as that of JAVA, let's revise it in python. Arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. 358 | 359 | \textbf{Arithmetic} operators are evaluated in the following order of precedence. 360 | 361 | \begin{center} 362 | \begin{tabular}{||c | c | c||} 363 | \hline 364 | \textbf{JAVA} & \textbf{Python} & \textbf{Description}\\ [1ex] 365 | \hline\hline 366 | NOT AVAILABLE & ** & Exponentiation\\ 367 | \hline 368 | - & - & Unary negation\\ 369 | \hline 370 | * & * & Multiplication\\ 371 | \hline 372 | / & / & Division\\ 373 | \hline 374 | \% & \% & Modulus arithmetic\\ 375 | \hline 376 | + & + & Addition\\ 377 | \hline 378 | - & - & Subtraction\\ 379 | \hline 380 | \end{tabular} 381 | \end{center} 382 | 383 | \textbf{Logical} operators are evaluated in the following order of precedence. 384 | 385 | \begin{center} 386 | \begin{tabular}{||c | c | c||} 387 | \hline 388 | \textbf{JAVA} & \textbf{Python} & \textbf{Description}\\ [1ex] 389 | \hline\hline 390 | ! & not & Logical negation\\ 391 | \hline 392 | \& & \& & Logical conjunction\\ 393 | \hline 394 | | & | & Logical dis-junction\\ 395 | \hline 396 | $\wedge$ & $\wedge$ & Logical exclusion \\ 397 | \hline 398 | \&\& & and & Conditional AND\\ 399 | \hline 400 | || & or & Conditional OR\\ 401 | \hline 402 | \end{tabular} 403 | \end{center} 404 | 405 | \textbf{Comparison} operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. 406 | 407 | \begin{center} 408 | \begin{tabular}{||c | c | c||} 409 | \hline 410 | \textbf{JAVA} & \textbf{Python} & \textbf{Description}\\ [1ex] 411 | \hline\hline 412 | < & < & Less than\\ 413 | \hline 414 | > & > & Greater than\\ 415 | \hline 416 | <= & <= & Less than or equal to \\ 417 | \hline 418 | >= & >= & Greater than or equal to\\ 419 | \hline 420 | == & == & Equality\\ 421 | \hline 422 | != & != & Inequality\\ 423 | \hline 424 | equals & is & Object equivalence \\ 425 | \hline 426 | \end{tabular} 427 | \end{center} 428 | \textbf{Note: } == in python is actually .equals() of Java. 429 | \vspace{5 mm} 430 | 431 | \section{Conditionals} 432 | \subsection{if} 433 | \begin{lstlisting} 434 | var1 = 250 435 | if 250 == var1: 436 | print ("The value of the variable is 250") 437 | \end{lstlisting} 438 | \vspace{5 mm} 439 | 440 | \subsection{if..else} 441 | \begin{lstlisting} 442 | var1 = 250 443 | if 0 == var1: 444 | MyLayerColor = 'vbRed' 445 | MyObjectColor = 'vbBlue' 446 | else : 447 | MyLayerColor = 'vbGreen' 448 | MyObjectColor = 'vbBlack' 449 | 450 | print (MyLayerColor) 451 | \end{lstlisting} 452 | 453 | \subsection{if..elif..elif..else} 454 | \begin{lstlisting} 455 | var1 = 0 456 | if 0 <= var1: 457 | print ("This is the first " + str(var1)) 458 | elif 1 == var1: 459 | print ("This is the second " + str(var1)) 460 | elif 2 >= var1: 461 | print ("This is the third " + str(var1)) 462 | else: 463 | print ("Value out of range!") 464 | \end{lstlisting} 465 | 466 | \subsection{Multiple conditions} 467 | \begin{lstlisting} 468 | skill1 = "java" 469 | skill2 = "python" 470 | 471 | if skill1 == "java" and skill2 == "python": 472 | print ("Both the condition satisfy") 473 | 474 | if skill1 == "java" or skill2 == "python": 475 | print ("At least One condition satisfies") 476 | 477 | \end{lstlisting} 478 | 479 | 480 | \section{Looping} 481 | 482 | \subsection{For Loop} 483 | Python will automatically increments the counter (x) variable by 1 after coming to end of the execution block. 484 | \begin{lstlisting} 485 | for x in range(0, 5): 486 | print ("It's a loop: " + str(x)) 487 | \end{lstlisting} 488 | 489 | Increase or decrease the counter variable by the value you specify. 490 | \begin{lstlisting} 491 | # the counter variable j is incremented by 2 each time the loop repeats 492 | for j in range(0, 10, 2): 493 | print ("We're on loop " + str(j)) 494 | 495 | # the counter variable j is decreased by 2 each time the loop repeats 496 | for j in range(10, 0, -2): 497 | print ("We're on loop " + str(j)) 498 | \end{lstlisting} 499 | 500 | You can exit any for loop before the counter reaches its end value by using the \boxed{break} statement. 501 | 502 | \subsection{While Loop} 503 | Simple example of while loop. 504 | \begin{lstlisting} 505 | var1 = 3 506 | while var1 < 37: 507 | var1 = var1 * 2 508 | print (var1) 509 | print ("Exited while loop.") 510 | \end{lstlisting} 511 | \vspace{5 mm} 512 | Another example of while loop with break statement. 513 | \begin{lstlisting} 514 | while True: 515 | n = raw_input("Please enter 'hello':") 516 | if n.strip() == 'hello': 517 | break 518 | \end{lstlisting} 519 | 520 | \section{Iterations} 521 | 522 | \subsection{Iterating a List} 523 | \begin{lstlisting} 524 | friends = ['Huey', 'Dewey', 'Louie'] 525 | for friend in friends: 526 | print ('Hello ', friend) 527 | print ('Done!') 528 | \end{lstlisting} 529 | 530 | \subsection{Iterating a Tuple} 531 | \begin{lstlisting} 532 | tup = ('alpha', 'beta', 'omega') 533 | for val in tup: 534 | print (val) 535 | \end{lstlisting} 536 | 537 | \subsection{Iterating a Dictionary} 538 | \begin{lstlisting} 539 | codes = {'INDIA': 'in', 'USA': 'us', 'UK': 'gb'} 540 | for key in codes: 541 | print (key, 'corresponds to', codes[key]) 542 | 543 | # Note: key is just a variable name. 544 | \end{lstlisting} 545 | The above will simply loop over the keys in the dictionary, rather than the keys and values. 546 | 547 | To loop over both key and value you can use the following: 548 | \begin{lstlisting} 549 | codes = {'INDIA': 'in', 'USA': 'us', 'UK': 'gb'} 550 | for key, value in codes.iteritems(): 551 | print (key, 'corresponds to', value) 552 | 553 | # Note: key, value are just the variable names. 554 | \end{lstlisting} 555 | 556 | \section{The End} 557 | That's all Folks.\newline 558 | This ebook is made using \href{https://overleaf.com}{Overleaf}. \newline The source can be found on \href{https://github.com/akashp1712/python_cheat_sheets}{\LARGE{\faGithub}}. \newline 559 | Thanks to \faLinkedin{} \href{https://www.linkedin.com/in/piyush-joshi-b7525216/}{Piyush Joshi} for all the guidance for this project. 560 | 561 | \end{multicols*} 562 | \end{document} 563 | --------------------------------------------------------------------------------