DEV Community

Cover image for 2.1 Python basics
Mohit Raj
Mohit Raj

Posted on • Updated on

2.1 Python basics

Hello guys,
From this post i am going to start Section-2 (Python basics). If you did'nt
read the previous post regarding the installisation of python then i suggest first go through this provided link https://dev.to/mohit355/download-and-install-python-3-x-1boo

Now,let's start,

Print() function

=>> This function is used to print something on the screen. One example shown in the previous post.
=>> Syntax : print(" ") or print(' ')
=>> you can use both single quotes or double quotes but one at a time.

=>> Inside the double quotes you can write any thing can be printed on the console screen (if you are using any IDE)
if you are using the Python IDLE then it will be printed on the same screen.

Let's take an example to explain what print function does. I want to print HELLO WORLD on the screen.
so i write print("HELLO WORLD") or print('HELLO WORLD') and it will print it on the screen .
Alt Text

String : Collection of characters inside "Double quotes" or 'Single quotes'

=>> Since we print HELLO WORLD, actually it is a string and that's why we write it in a single or double quotes.

Some Point to Know -

  1. If you are using double quotes then inside the double quotes you can use single quotes and vice versa
    Alt Text

  2. double quotes or single quotes are not used inside double quotes or single quotes respectively
    otherwise it will show you an error (Syntax error)
    Alt Text

Comment in Python

  1. Comment is for the user who is going to read the written code, comments make easy for the reader to understand the code
  2. comment is ignored by the Python interpreter means when you run your program comment line cannot be treated as line of code it just ignored (that's why this is only for the user)
  3. Comments in Python start with the hash character (#) , by using hash we can comment a single line
    Alt Text

  4. if you want to comment multiple line then use triple quotes (''') at the beginning of the comment and at the end of the comment

  5. A comment may appear at the start of a line or following whitespace or code, but not within a string literal.
    A hash character within a string literal is just a hash character.

Escape sequence in Python

  1. Escape characters are characters that are generally used to perform certain tasks and their usage in code directs the compiler to take a suitable action mapped to that character.

2.In Python strings, the backslash "\" is a special character, also called the "escape" character.
It is used in representing certain whitespace characters
Example of Escape characters :-
a) \ newline ( Backslash and newline ignored)
Alt Text

b) \ ( Backslash () )
c) \' ( Single quote (')
Alt Text

d) \" ( Double quote (") )
e) \b ( ASCII Backspace (BS) )
Alt Text

f) \n ( ASCII Linefeed /Break the line(LF) )
g) \t ( ASCII Horizontal Tab (TAB) )
Alt Text

h) \000 ( Character with octal value 000 )
(000 may be any integer value according to octal value )

Suppose we want to print Hello using octal value of H-110,e-145,l-154,157
so we can write print("\110\145\154\154\157") to print Hello on the screen
Alt Text

r(raw string)

   used to print every thing inside the quotes means is there is any escape character inside then r (raw string) make it netural and 

behaves as a simple character
Example: print(r"hello \nworld)
Alt Textit will print as it is

That's all for this. if you have any problem regarding escape character or any thing please let me know in comment box

Top comments (2)

Collapse
 
transfxed profile image
K I M S A N T O S

Thank you!

Collapse
 
mohit355 profile image
Mohit Raj

Your Welcome