Posted by admin at March 11, 2020
Like all high-level languages, Python is easy to read, takes less time to write, and is portable. This versatile programming language has two versions: Python 2 and Python 3. Wiki says: Python 2.x is legacy, Python 3.x is the present and future of the language. That is, Python 2 is no longer in development and all new features will be added in Python 3. Note that, keeping this in mind, the code examples in this tutorial are in Python 3. Wherever Python 2.x code is shown, it will be highlighted.
A function is defined as a block of organized, reusable code used to perform a single, related action. Python has many built-in functions; you can also create your own. Python has an input function which lets you ask a user for some text input. You call this function to tell the program to stop and wait for the user to key in the data. In Python 2, you have a built-in function
raw_input(), whereas in Python 3, you have
input(). The program will resume once the user presses the ENTER or RETURN key. Look at this example to get input from the keyboard using Python 2 in the interactive mode. Your output is displayed in quotes once you hit the ENTER key.
>>>raw_input() I am learning (This is where you type in) 'I am learning (The interpreter showing you how the input is captured.)
In Python 3.x, you need to use input().
>>> input() I am learning 'I am learning
You can always tell your user what to input by printing a prompt. There is no difference between
input in Python 3 and
raw_input in Python 2 except for the keywords.
To output your data to the screen, use the
print() function. You can write
print(argument) and this will print the
argument in the next line when you press the
Definitions to remember: An argument is a value you pass to a function when calling it. A value is a letter or a number. A variable is a name that refers to a value. It begins with a letter. An assignment statement creates new variables and gives them values.
This syntax is valid in both Python 3.x and Python 2.x. For example, if your data is “Guido,” you can put “Guido” inside the parentheses
( ) after
>>> print("Guido") Guido
To capture the input in your program, you will need a variable. A variable is a container to hold data. (You will learn more on variables in a later tutorial.) You can take the input and assign it to a variable. This is done using the
= operator before the input keyword and then putting the variable name before the
= operator. For example, when you give a sentence “generic input” as the input, this gets assigned to a variable, say,
my_var. You can then print the value stored in
my_var. Let us understand this with the following example:
>>> # take an input and assign it to a variable >>> beautiful_number = input() # The data you key in the next line which is 6 will be assigned to beautiful_number 6 >>> print(beautiful_number) # the next line will print the value in beautiful_number after you press enter or return '6'
It is often a good idea to tell the user what to input. You can do this by putting the hint in quotes inside the input parentheses. The hint will come in the next line and will wait for the user input. You can then type the input and when you hit the ENTER key, it will capture the input. In this example, “tell me a beautiful number” is the hint. This gets printed in the next line when asking for the input. If you type 6, this will be assigned to the variable
beautiful_number which we can print later.
>>> beautiful_number = input("tell me a beautiful number ") tell me a beautiful number 6 >>> print(beautiful_number) '6'
Say you want to print a specific string (a sequence of characters such as letters, punctuation marks, numbers, and letters) N number of times. The (asterisk) * operator performs repetition on strings. You can print “5” six times. Inside the print parentheses, put â€œ5â€ followed by
* and the number of times you want â€œ5â€ repeated.
>>> print("5"*6) 555555
You can separate the output using the comma delimiter. By default, this adds a space between the output items. For example, the sequence of numbers
5,6,7 separated by comma
, gets printed with a space between one number and the next.
>>> print(5,6,7) 5 6 7
To change the output to what you want, use the keyword arguments sep and end to print ( ). When separating the output with a comma delimiter, you can also define the separation format using the â€œsepâ€ keyword.
>>> print('LOVE', 30, 82.2) LOVE 30 82.2 >>> print('LOVE', 30, 82.2, sep=',') 'LOVE', 30, 82.2
>>> print('LOVE', 30, 82.2, sep=',', end='!!\n') 'LOVE', 30, 82.2!!
For example, you can print the letters in the word “python” and all the letters will come in a new line.
>>> for i in "python": ... print(i) ... p y t h o n
You can change this default implementation. You can have a colon
: between the letters instead of a new line.
>>> for i in "python": ... print(i, end=":") ... p:y:t:h:o:n:
Say you can assign the number
7 to a variable
population and if you write the logic
population * 7 inside the parentheses of
>>> population = 7 >>> print("Population in 2050: ", population * 1.28) # making the calculation in place Population in 2050: 8.96