Getting Started with python

 Python is a very popular programming language. The programmer who masters it has many possibilities and its syntax is the basis of other very popular languages. In order for you to start mastering it, the first thing you should know is precisely that, its basic syntax. If you already have some programming notions, the learning curve will be easier for you. We will try to be as precise as possible so that you can start off on the right foot:


 How to get started?



 We have 2 ways of programming with Python: • Interactive mode: This mode executes the orders at the moment we indicate them; that is, it carries them out in real-time. In order to work with this mode, you will have to activate the interpreter from the command line. As soon as we have it done, we can do the usual "Hello, World" print test. • Script mode: Interactive mode is the best to start with, but it won't take long for us to realize that we are going to be a little short. Later on, it will be much more practical to create scripts, which will be like blocks that contain all the commands that we need. We will use this command to execute them -> Python example.py.




Installing


You can easily install python from its official website. One of the common problems I have seen amongst windows users is that they get a message like pip not found. If you get something similar like that then take a look at fixing pip not recognized 



 Identifiers



 Identifiers, as their name suggests, are used in Python to identify certain parts of the code (such as variables, functions, expressions, statements, modules, objects, or classes). The most common is that they start with letters, with a hyphen preceded by zeros and different alphanumeric codes. We will have to remember these rules about identifiers, as they will allow us to better treat the code: -A class name must always start with an uppercase letter. -If it is not a class name, it must start with a lowercase letter. -In the case that there is an underscore at the beginning of the identifier, this tells us that it is private. -If the end of the identifier consists of two underscores, the code tells us that it is a special identifier reserved by the language. Variables are memory spaces reserved for a value that can change according to certain criteria. The objects are simple components that have associated a group of values ​​or operations. Expressions are a combination of values, variables, constants, operations ... that are carried out according to different syntax rules. Modules are sets of sentences that will be ordered according to a specific purpose. 



Indents



 To indicate code blocks with Python we will have to use line indentation, applied rigidly. It will be necessary to consider that the amount of spaces that can be applied in the indentation is versatile; however, the block declarations must be indented correctly, with the same amount. We will see this in this example. 


* This is the correct way to indent an IF: if True: print "True statement" else: print "False statement" 

* This is the wrong way to indent an IF: if True: print "True" else: print "False" In the latter case, Python will give us an error. 


Reserved Words in Python



 As in any programming code, in Python, it will be necessary to work with constants and variables. However, we cannot use any type of word, as some are restricted by language, as they will be used in another way. These are the words reserved by the system: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield. If you've used a programming language before, you will recognize most of the reserved words. Declarations in more than one line The most common thing is that the programmer ends the declarations with only one line ... however, we can also use several lines, as long as the identifier is used.


 Example: Variable = Value1 + Value 2 + Value_3 + 



Comments



 Comments are very handy to guide us through the code. Knowing how to identify them will help us to know the operation of certain functionalities, or to clarify certain eventualities. The way to write a comment is by using the # character. Important: The comments are not interpreted by Python, they only serve to inform the programmer. Example: 


Print “Hello, World” # It will write “Hello, World” on the screen

Quotes -> Quotes To create quotes with Python we will use single or double-quotes. Quotation marks are used to display text on the screen, or for certain commands. In the event that we need to quote a longer text, it is best to use triple quotation marks (this allows us to quote texts of several lines). -The quotes are usually used for only one word (Variable = 'variable'). With these basic notions, you can start programming in Python.

Comments

Popular posts from this blog

Numbers in Python

5 harmless ways to kick spam out of your blog