Ask coding questions

← Back to all posts
Can anyone give some tags of python
RamBarabdeCoder (1)

please explain me the python language'

Comments
hotnewtop
InvisibleOne (3224)

Ok, here's an overview.

Variables:

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.

variable_1 = value

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.

Some examples:

variable_int = 24
variable_float = 12.0032
variable_string = "This is a string!"
variable_list = ["List of strings", "numbers and floats", 20, 20.22]

Comments

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 '#'

variable = 10
# this is a comment explaining how the variable is equal to the integer 10

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.

"""
Multiline Comment
In python....
Cool right?
"""

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:

print("Hello, world!")

or that could be a variable holding a string

my_string = "Hello, World!"

print(my_string)

You can also print numbers with print

print(10)

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:

number = 25

print("You have " + str(number) + " dollars")

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:

name = input("What's your name? ")

print("Hello, " + name)

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 ==

Like this:

if 1 == 1:
  print("1 is equal to 1")

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

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:

import random

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:

number = random.randint(1, 10)

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:

from random import randint

now we only have the randint function and we don't have to reference random first. We can just type:

number = randint(1, 10)

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:

from random import *

number = randint(1, 10)

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.

Mini Project

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:

  1. Import random and store a random number in a variable
import random

secret_number = random.randint(1, 10)
  1. Make a variable to hold how many guesses the user has used
guesses = 0
  1. Start a while loop that asks the user for input, and then converts that input to an integer with int()
while 1==1: # we can shorten this to while True: since True is always True
  user_guess = input("Guess the number: ")

  user_guess = int(user_guess)
  1. Check if the user guess is equal to our secret number
if user_guess == secret_number:
  print("You won!")
  print("You took " + str(guesses) + " guesses to guess my number")
  break   # stops the loop from running
else:
  print("That's wrong! Try again")
  
  # add one to their guess count

  guesses += 1
  

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.

HackermonDev (2074)

It's a programming language. You can learn more from its official website.