ads

Python Basics - Kickstart to Python (Python for Hackers Part-3)

Your goal, of course, is to make Python go beyond printing a text. To do that, you will need to learn other concepts that are essential in a Python script. You will also want to create a script that is easy for you to understand and review in the future, just in any case you want to improve it and turn it into a working tool for your hacks.


In order to take inputs and manipulate them in order to get certain results, you will first need to learn how variables and constants work in this programming language.

Comments

These are statements that come after the '#' symbol. These pieces of texts allow you to:

•Explain the problems that you are aiming to overcome or solve in your program.

•Take note of the important assumptions, details, and decisions that you want to perform in the code.

Making notes in your code does not only remind you what you want to achieve in your code, but also help readers that will be using your program understand what lines of code are supposed to do.

Literal Constants

Literal constants are named as such because you take these pieces of text for their literal value. These constants can be:

•Numbers

They can be integers (plain whole numbers) or floats (numbers that have decimal points)

•Strings

These are sequences of characters, which you can specify using single quote, double
quotes, or triple quotes. Take note that single and double quotes function similarly in Python, and that you can express them freely inside triple quotes.

Here is an example:


Strings are also immutable, which means that you cannot change a string once you have created it.

How to Format Strings

There are instances in which you will want to construct strings from a different piece of information. To do this, you will need to use the method. Take a look at this example: 

Once you are done, save this piece of code as str_format.py. This is how it’s going to
look like when you run the program:


The format method allows you to use an argument value to take the place of a particular specification. Take a look at this example:


This piece of code will give you this result:


Variables

Because there will be multiple instance wherein you will need to store information in your code and then manipulate them, you will need to have some variables. Just like what the name means, variables have varying values, such as real numbers, strings, Booleans, dictionaries, or lists, which you can access through certain methods. Take a look at this sample code:


In this example, you are able to define the variable named port, which is going to be used to store the integer 21, and the variable named banner, which is going to hold a string. In order to combine these variables together as a single string, you will need to use the variable port through the use of the str() function.

Since you need to quickly access the data you stored, you need to assign names to variables. This is where identifiers come to play. Identifiers work like code names that you use to point out to something that you have used in your code or program. Here are some rules that you need to follow when assigning them: 


•The initial character should be a letter of the alphabet or an underscore.

•The remaining characters should consist of underscores, letters, or digits

•They are case-sensitive, which means that mycode and myCode do not call out the same value and not interchangeable when you assign them as an identifier.

Objects

Things that are referred to as anything in the code that exists in Python are called objects. If you are migrating to Python from another programming language, you need to take note that everything in Python, including string, numbers, and functions, is classified as an object.

Post a Comment

0 Comments