Printing and variables in Python - ProgrammerTech


Printing and variables in Python

  • Share this:
Printing and variables in Python

Printing in Python

Printing in Python There are many ways to print and we will take many examples of printing in Python. We will reprint with you the sentence that we used in the previous lesson, and our whole lesson today will be examples in print.

Example 1

Print the word programmer tech using the print function in Python.

>>> print ("programmer tech")
programmer tech

We note that we typed print and then we opened parentheses and inserted these parentheses as quotation marks and the text to be printed, and when we pressed Enter, it printed between the quotation marks.

Example 2

Print the number 1 twice, in the first operation it is an integer and in the second operation it is in the form of text.

>>> print (1)
>>> print ("1")

We notice in the image that we used two examples and they gave us the same result, but the difference between the first and second results is that in the first operation we did not put quotation marks, and the interpreter considered this as an integer, and in the second process we put quotation marks here, which is considered a String text.

Example 3

Combine the letters M and S together by using the print function.

>>> print ("M" + "S")

We notice in this example that we put quotation marks in this process, so he read it as text and not a process of adding and merging the two letters next to each other.

Example 4

Type your name, then drop a line, and write your age on the second line.

>>> print ("i am data\n age 26")
i am data
age 26

In this example, we used the \ n which is the descent of a new line. We wrote i am data and put the \ n and completed the sentence. We noticed that it was downloaded to us with a new line.

Example 5

Type the word programmer tech and leave 4 blank spaces between the two words without using the spaces using the ruler on the keyboard.

>>>print ("programmer\ttech")
programmer    tech

In this example we used \t to mean leaving a space of 4 spaces in the sentence as when pressing the tab button.

Example 6

Print the data sentence and Caesar are the administrators of the technical programmer's site, provided that every word is on a line, use the back slash \ in the print format.

>>>print ("data and kaissar\
... manager \
... website \
... programmer tech \
... ")
data and kaissar manager website programmer tech

In this example, we used the back slash \ after the end of each line, and the slash signal did not appear because we told him we wanted to write a new line.

Example 7

Type hello my friends at the technical programmer's site while keeping the text formatting using double quotes three times.

>>>print (""" hello
... my friends
...programmer tech
my friends
programmer tech

In this example, we put six quotation marks """""" and in the middle of them we wrote the words we want without using the back slash as we want and finally prints the sentence as we wrote it until empty lines come down.

Example 8

Use individual comments via # in speech.

>>>print ("hello")   #this is hello example
>>>print ("hello")

In the previous example, we used the # sign, which is the comment flag. In the first example, we wrote the comment after the print command, and we noticed that the comment did not appear to us, and in the second example, we put the comment sign before the print command and nothing appeared because it considered it a comment and ignored it.


variables in Python

Variables Python is considered as a box that holds data, and we specify a name for this box and we call it whenever we want with the name we set for it. The definition of the variable in Python, for example, is a text variable name, meaning the name of the variable, and after it the = sign, and after it two quotation marks “” and we write within the quotation marks the value of the variable.

Example 1 if the value is string

>>>name = "programmer tech"
'programmer tech'

Note that in this example we have given a variable name and the value of this variable is programmer tech, and when we call the variable name directly, the value of programmer tech is printed for us.

Example 2 If the value is integer

>>>age = 26

We notice, as in the previous example, that we named the variable age and gave it the value 26, and when we wrote the name of the variable, it printed the value 26.

Example 3

>>>name = "programmer tech"
'programmer tech'
>>>age = "26"
>>>print (name)
'programmer tech'

We notice in this image that when we used the print function and typed the name of the variable directly, it was known that we wanted to print the value of the variable name and printed it to us on the screen, but here the 26 is considered a text value because we put it in quotation marks.

Example 4 Add all the values ​​of the variable with one line

>>>name, age, info = "programmer",26,"programmer web"
'programmer web'

We note that we wrote three variables with one line with the values ​​separated by a comma, and when we called name it gave us the value and the same thing for the other variables age, info, and we can call the values ​​with the print function also, for example print (info) will print the info value for us ... ..etc.

Example 5

>>>print ("i am %s and my age %d and info is %s"% (name,age,info))
i am programmer and my age 26 and info is programmer web
  • The %s sign means that this is a text value.
  • The %d sign is a numeric value.

We wrote for the program to print i am and then we put the sign of the text value and then my age and then we put the sign of a numeric value because the value of age is a number and then. We typed and info is and a string value sign and we opened a parenthesis. We defined the variables as age,name,info, and when we pressed IN, it printed the sentence for us, and in place of the values ​​tags, the values ​​we set before were printed.


Using format in Python

The format method performs formatting operations on strings, and the string can contain regular characters or replacement fields enclosed in braces {}.

Example 6

>>>print ("i am {0} and my age {1} and info is {2}".format(name,age,info))
i am programmer and my age 26 and info is programmer web

We see from the output command that it has beautifully formatted the string for us.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.