Introduction to the python - Letsprogram

Share:
Basic concepts in python

Comments in Python start with the hash character, #, and extend to the end of the physical line. 
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. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples.

# this is the first comment                                             
spam = 1  # and this is the second comment                              
          # ... and now a third!                                        
text = "# This is not a comment because it's inside quotes."            

Let’s try some basic Python commands. Start the interpreter and wait for the python prompt, >>>. 


Numbers

The equal sign (=) is used to assign a value to a variable. Afterward, no result is displayed before the next interactive prompt. In Python, we need no to define the variable before using it. But If a variable is not “defined” (assigned a value), trying to use it will give you an error

Using  (/)  gives you float value of remainder using  (//) gives integer value of the remainder.

In addition to int and float, Python supports other types of numbers, such as Decimal and Fraction. Python also has built-in support for complex numbers and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).


All concepts have an example given below

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
#..........................................#
>>> n # try to access an undefined variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
#.........................#
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>> 4 * 3.75 - 1
14.0



Strings

  • ( \ ) is used to escape quotes in a string.
  • You can use raw strings by adding an r before the first quote it makes sure to ignore the meaning of ( \n ).
  • Strings can be concatenated (glued together) with the + operator, and repeated with *
  • Two or more strings beside each other are concatenated automatically.


All concepts have an example given below

>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line
>>> print('C:\sairam\naveen') # here \n means newline!
C:\sairam
aveen
>>> print(r'C:\sairam\naveen') # note the r before the quote
C:\sairam\naveen
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium' # strings can be concantenated by using +,*
'unununium'
>>> 'Py' 'thon'
'Python'
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
>>> prefix + 'thon'
'Python'

Post a Comment

F