Can anyone give some tags of python
please explain me the python language'
Ok, here's an overview.
Variables store blocks of data for use in the code, to create a variable you give it a name, then the value it is equal to.
Variables can either be integers, which are numbers without a decimal point.
Floats, a number with a decimal point
Strings, which is anything between two quotes, usually like a string of text
There are also lists, which hold lists of values inside of two brackets, each value is separated by a comma.
You use comments in code as something to help you understand the code when you come back to it. Comments are invisible for the computer and are basically just notes.
In python, you start a single line comment with '#'
You can make multiline comments in python, which is actually just a multiline string.
To make a multiline string you start with three quotes, and end with three quotes. To make the string a comment just don't set it equal to a variable.
Print and Input
To display data to the console, we use
print() and inside the parenthesis we put what we want to print.
That could be a string:
or that could be a variable holding a string
You can also print numbers with print
But you cannot print numbers and a string, as that will cause errors. If you want to print a number and a string, you have to convert the number to a string so it can be combined with your other string.
You can convert a number to a string with
str() putting the number between the parenthesis.
That way you can do this:
Input is how we get an input back from the user, and then we store that input in a variable for later use.
the input function looks like this:
input() and between those parenthesis we can put a prompt that will be printed to the screen if we like.
Here's an example:
If and Else
If checks if an expression is true, if it is, it does the code indented below if, if it does not, then it will run the code indented under else.
Using if, we can check if a value is equal to something, we do this with the double equal sign operator
Any code intended under if will be run. If the expression is not true, then the code will look for an else statement. If there is no else, nothing will happen, if there is, then it will run the code indented under the else.
I'm probably explaining it terribly but you'll understand it better once you use it a bit.
import is super useful in python, it allows you to import other python files and use the functions inside of those files in your code.
For example, maybe we want to get a random number, we can do that by importing a python library called
random like this:
and then we can use the function inside of random
randint() to get a random number, first by referencing random, and then the function we want to use, like this:
This will give us a random number from 1 to 10
But maybe we only want to use the
randint() number and we don't want all the other functions inside of random.
We can do that by importing randint from random, which looks like this:
now we only have the randint function and we don't have to reference random first. We can just type:
If we want to import everything from a library and not have to reference it first, we can put
* which imports all the functions from a library, like this:
But usually, it's a good idea just to
import random and do
random.randint(1, 10) just so that you know where you are getting those functions from so we don't get mixed up.
From that quick overview, you should have a basic idea of some python, now let's make a little project.
In this code, we want to store a random number in a variable, then ask the user to guess that number. If they are correct, we tell them so, if they are not, then they have to keep on guessing.
All this code will be inside of a
while loop, which is something I haven't covered yet.
A while loop, will always run the code below it again and again
while the expression is true.
The code will look like this:
- Import random and store a random number in a variable
- Make a variable to hold how many guesses the user has used
- Start a while loop that asks the user for input, and then converts that input to an integer with
- Check if the user guess is equal to our secret number
And there you go, run the code, and hopefully it should work. If it doesn't then I probably did something wrong, so let me know and I'll fix it.