Example: air traffic controller

Python cheat sheet April 2021 - WebsiteSetup

PythonCheat SheetPython 3 is a truly versatile pro rammin lan ua e, loved both by web developers, data scientists and software en ineers. And there are several ood reasons for that!Once you et a han of it, your development speed and productivity will soar! Python is open-source and has a reat support community, Plus, extensive support libraries. ts data structures are asics ettin tarted ain Python ata ypes ow to reate a trin in Python ath perators ow to tore trin s in ariables uilt-in unctions in Python ow to efine a unction ist ist omprehensions uples ictionaries f tatements onditional tatements in PythonPython oops lass ealin with Python xceptions rrors ow to roubleshoot the rrors onclusion030405060708 0 6 6 7 3 4 5 a le of ontents ython asics ettin tarted hat is nte rated evelopment and earnin ost indows and ac computers come with Python pre-installed. ou can chec that via a ommand ine search.

Python Cheat Sheet Python 3 is a truly versatile prorammin lanuae, loved both by web developers, data scientists and software enineers. And there are several ood reasons for that! Once you et a han of it, your development speed and productivity will soar!

Tags:

  Python, Sheet, 2012, April, Teach, Python cheat sheet april 2021, Python cheat sheet python

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Python cheat sheet April 2021 - WebsiteSetup

1 PythonCheat SheetPython 3 is a truly versatile pro rammin lan ua e, loved both by web developers, data scientists and software en ineers. And there are several ood reasons for that!Once you et a han of it, your development speed and productivity will soar! Python is open-source and has a reat support community, Plus, extensive support libraries. ts data structures are asics ettin tarted ain Python ata ypes ow to reate a trin in Python ath perators ow to tore trin s in ariables uilt-in unctions in Python ow to efine a unction ist ist omprehensions uples ictionaries f tatements onditional tatements in PythonPython oops lass ealin with Python xceptions rrors ow to roubleshoot the rrors onclusion030405060708 0 6 6 7 3 4 5 a le of ontents ython asics ettin tarted hat is nte rated evelopment and earnin ost indows and ac computers come with Python pre-installed. ou can chec that via a ommand ine search.

2 He particular appeal of Python is that you can write a pro ram in any text editor, save it in .py format and then run via a ommand ine. ut as you learn to write more complex code or venture into data science, you mi ht want to switch to an or . nte rated evelopment and earnin nvironment comes with every Python installation. ts advanta e over other text editors is that it hi hli hts important eywords e.. strin functions , ma in it easier for you to interpret code. hell is the default mode of operation for Python . n essence, it s a simple loop that performs that followin four steps eads the Python statement valuates the results of it Prints the result on the screen And then loops bac to read the next statement. Python shell is a reat place to test various small code cheat - Python cheat sheet ain ython ata ypes very value in Python is called an o ect . And every ob ect has a specific data type.

3 He three most-used data types are as follows nte ers int an inte er number to represent an ob ect such as number 3 . trin s codify a se uence of characters usin a strin . or example, the word hello . n Python 3, strin s are immutable. f you already defined one, you cannot chan e it later on. hile you can modify a strin with commands such as replace or oin , they will create a copy of a strin and apply modification to it, rather than rewrite the ori inal , another three types worth mentionin are lists, dictionaries, and tuples. All of them are discussed in the next sections. or now, let s focus on the strin s. loatin point num ers float use them to represent floatin -point , -1, 0, 1, 2, 3, 4, 5 Strings yo , hey , Hello! , what s up! Floating-point numbers , , , , , , cheat sheet - Python cheat sheet ow to reate a trin in ython asic ython trin trin oncatenation ou can create a strin in three ways usin sin le, dou le or triple uotes.

4 Ere s an example of every option ! hichever option you choose, you should stic to it and use it consistently within your pro ram. As the next step, you can use the print function to output your strin in the console window. his lets you review your code and ensure that all functions well. ere s a snippet for that my_string = Let s Learn Python ! another_string = It may seem difficult first, but you can do it! a_long_string = Yes, you can e en master multi line strings that co er more than one line ith some ractice he next thin you can master is concatenation a way to add two strin s to ether usin the operator. ere s how it s done ote ou can t apply operator to two different data types e.. strin inte er. f you try to do that, you ll et the followin Python error string_one = I m reading string_t o = a ne great boo ! string_three = string_one string_t o y e rror an t con ert int ob ect to str im licitly rint Let s rint out a string!

5 Python cheat sheet - Python cheat sheet trin eplication ath OperatorsAs the name implies, this command lets you repeat the same strin several times. his is done usin operator. ind that this operator acts as a replicator only with strin data types. hen applied to numbers, it acts as a multiplier. trin replication example or reference, here s a list of other math operations you can apply towards numbers And with print And your output will be Alice written five times in a row. lice lice lice lice lice lice rint lice OperatorsOperationExample**Exponent2 ** 3 = 8%Modulus/Remainder22 % 8 = 6//Integer division22 // 8 = 2/Division22 / 8 = *Multiplication3 * 3 = 9-Subtraction5 - 2 = 3+Addition2 + 2 = 4 Python cheat sheet - Python cheat sheet ow to tore trin s in aria les aria les in Python 3 are special symbols that assi n a specific stora e location to a value that s tied to it. n essence, variables are li e special labels that you place on some value to now where it s stored.

6 Trin s incorporate data. o you can pac them inside a variable. oin so ma es it easier to wor with complex Python pro rams. ere s how you can store a strin inside a variable. et s brea it down a bit further my str is the variable name. is the assi nment operator. ust a random strin is a value you tie to the variable name. ow when you print this out, you receive the strin output. ee y usin variables, you save yourself heaps of effort as you don t need to retype the complete strin every time you want to use = ello orld rint my_str ello orldPython cheat sheet - Python cheat sheet uilt in unctions in ython nput unctionlen unction ou already now the most popular function in Python print . ow let s ta e a loo at its e ually popular cousins that are in-built in the platform. hen you run this short pro ram, the results will loo li e this utput input function is a simple way to prompt the user for some input e.

7 Provide their name . All user input is stored as a strin . ere s a uic snippet to illustrate this len function helps you find the len th of any strin , list, tuple, dictionary, or another data type. t s a handy command to determine excessive values and trim them to optimi e the performance of your pro ram. ere s an input function example for a strin i! hat s your name im ice to meet you, im! ow old are you 5 o, you are already 5 years old, im! he len th of the strin is 35name = in ut i! hat s your name rint ice to meet you name ! age = in ut o old are you rint o, you are already str age years old, name ! # testing len() str = o e you are en oying our tutorial! rint he length of the string is , len str Python cheat sheet - Python cheat Sheetfilter se the ilter function to exclude items in an iterable ob ect lists, tuples, dictionaries, etc ptional he P version of the chec list can also include a full table of all the in-built functions.

8 Ages = , , , , , def my unc if return alse else return rue adults = filter my unc, ages for in adults rint Python cheat sheet - Python cheat sheet ow to efine a unctionApart from usin in-built functions, Python 3 also allows you to define your own functions for your pro ram. o recap, a function is a bloc of coded instructions that perform a certain action. nce properly defined, a function can be reused throu hout your pro ram re-use the same code. ere s a uic wal throu h explainin how to define a function in Python irst, use def eyword followed by the function name . he parentheses can contain any parameters that your function should ta e or stay empty . ext, you ll need to add a second code line with a -space indent to specify what this function should do. ow, let s ta e a loo at a defined function with a parameter an entity, specifyin an ar ument that a function can accept.

9 Ow, you have to call this function to run the name def name rint hat s your name def add_numbers , y, a = y b = c = y rint a, b, c add_numbers , , name y def name rint hat s your name hello Python cheat sheet - Python cheat Sheetname() ow to ass eyword r uments to a unction n this case, you pass the number in for the x parameter, in for the y parameter, and 3 in for the parameter. he pro ram will that do the simple math of addin up the numbers utput A function can also accept eyword ar uments. n this case, you can use parameters in random order as the Python interpreter will use the provided eywords to match the values to the parameters. ere s a simple example of how you pass a eyword ar ument to a function. utput # Define function with parameters def roduct_info roduct name, rice rint roductname roduct name rint Price str dollars # Call function with parameters assigned as above roduct_info hite shirt , dollars # Call function with keyword arguments roduct_info roductname= eans , rice= roductname hite shirt rice 5 roductname eans rice 45a 3c 3 Python cheat sheet - Python cheat Sheet3 Product Name: White T-ShirtPrice: 15 Product Name: JeansPrice: 454defproduct_info(product name, price):print( Product Name: + product_name)print( Price: + str(price))5# Define function with parametersproduct_info( White T-Shirt.)

10 , 15)# Call function with parameters assigned as aboveproduct_info(productname= Jeans , price=45)# Call function with keyword arguments ists ample lists ow to dd tems to a ist ists are another cornerstone data type in Python used to specify an ordered se uence of elements. n short, they help you eep related data to ether and perform the same operations on several values at once. nli e strin s, lists are mutable chan eable . ach value inside a list is called an item and these are placed between s uare brac , you can use list function to do the same ou have two ways to add new items to existin lists. he first one is usin append function he second option is to insert function to add an item at the specified index my_list = , , my_list = a , b , c my_list = , d, boo , beta_list = a le , banana , orange beta_list a end gra e rint beta_list beta_list = a le , banana , orange beta_list insert gra e rint beta_list al ha_list = list , , rint al ha_list Python cheat sheet - Python cheat Sheet2, grape ow to emove an tem from a ist om ine wo ists reate a ested istA ain, you have several ways to do so.