TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login CONVERT LENGTH
login CONVERT TEMPERATURE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login WORK CALCULATOR
login CALCULATE LIFE

Python 3 programming language, learn python, tutorial

Previous articlePage bottomNext article  ALL TOPICS

Python 3. Variables, data types, comments, numbers, text

 
Reserved words
And exec Not
Assert finally or
Break for pass
Class from print
Continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
Standard Data Types
 
  • Numbers
  • String
  • List
  • Tuple
  • Dictionary
Numbers
  • int
  • long
  • float
  • complex
Operations
  • + Addition     
  • - Subtraction     
  • * Multiplication     
  • / Division     
  • % Modulus     
  • ** Exponent
  • // Floor Division
Lets use hello.py file again.

1. Here you can see comments and variable. The variable is vot declared, but interpreter understans it as text:
#  comment
spam = 1  #  comment
          #  comment
text = "# This is not a comment"

print (text)

Result:
# This is not a comment

2. Another example:
2 + 2

10 / 10 * 10

2**3 #degree

Result - empty:
 

There is no error, but there is no result too. Nothing is printed

3. Right example
print( 2 + 2 )

print ( 10 / 10 * 10 )

print ( 2**3 ) #degree

Result:
4
10.0
8

4. Fractions aren't lost when dividing integers. :
print ((50-5*6)/4)
print (8/5)

Result:
5.0
1.6

5. Integer division. It returns the floor:
print (7//3)
print (7//-3)

Result:
2
-3

6. Variables:
x = y = z = 1
print (x)
print (y)
print (z)

Result:
1
1
1

6a. Try:
a, b, c = 1, 2, "john"

print (a)
print (b)
print (c)

7. Unknown variable:
print (zz);

Result:
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    print (zz);
NameError: name 'zz' is not defined

8. Complex numbers:
print (1j * 1J)

print (1j * complex(0, 1))

print (3+1j*3)

print ((3+1j)*3)

print ((1+2j)/(1+1j))

a=1.5+0.5j
print (  a.real, a.imag )

print (abs(a)) # SQRT for triangle

Result:
(-1+0j)
(-1+0j)
(3+3j)
(9+3j)
(1.5+0.5j)
1.5 0.5
1.5811388300841898

9. Strings:
a =  'hello python'
print (a)

a = 'doesn\'t hello python'
print (a)

a = "doesn't hello python"
print (a)

a = '"Yes," for python.'
print (a)

a = "\"Yes,\" fort python."
print (a)

a = '"Yes," he said.'
print (a)

a = "\n\
first\n\
second\
third"

print(a)
 

Result:
hello python
doesn't hello python
doesn't hello python
"Yes," for python.
"Yes," fort python.
"Yes," he said.

first
secondthird

10. You get, what you write. Triple-quotes: """ or ''':
print("""\

Field 1 | Field 2
------------------
1       | 2                    
3       | 4      
------------------
""")

Result:
Field 1 | Field 2
------------------
1       | 2      
3       | 4      
------------------

11. Show special symbols (r):
a = r"\n\
first\n\
second\
third"

print(a)

Result:
\n\
first\n\
second\
third

11a. Try:
print (r"Hello, \n Python!")
 
print ("Hello, \n Python!")

12. Text and two variables:
a = 'Help' + 'A'
b = 'Help' + a
print(b)

Result:
HelpHelpA

13. Multiline:
total = "item_one " + \
        "item_two " + \
        "item_three "
        
print (total)


14. Strings:
py = 'Hello World!'

print (py)          # Prints complete string

print (py[0])       # Prints first character of the string

print (py[2:5])     # Prints characters starting from 3rd to 5th

print (py[2:])      # Prints string starting from 3rd character

print (py * 2)      # Prints string two times

print (py + "TEST") # Prints concatenated string

15.
S = "Spam"
S  = 'z' + S[1:]
print (S)

Result:
'zpam'


Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
One should take good care not to grow too wise for so great a pleasure of life as laughter.
Joseph Addison
Would you rather have twenty dollars or twenty dollars and fifty cents?" "I would rather have twenty dollars, because you don't have change. Plus you could have a ten, a five, and four ones.
Nate
Anyone who doesn't miss the past never had a mother.
Gregory Nunn