[Python][Basic] Basic Types

Programming

Types

Numbers

http://nbviewer.jupyter.org/github/jmportilla/Complete-Python-Bootcamp/blob/master/Numbers.ipynb

  • cast
    • int(1.5) => 1
    • float(1) => 1.0
  • Difference Python2 vs Python3
    • In Python 2, the / symbol performs what is known as “classic” division, this means that the decimal points are truncated (cut off).
      • 3/2 => 1
    • In Python 3 however, a single / performs “true” division. So you would get 1.5 if you had inputed 3/2 in Python 3.
      • 3/2 => 1.5
    • How to make the result of both versions become the same:
      1. add decimals on Numerator or denominator
        • 3.0/2 => 1.5
        • 3/2.0 => 1.5
      2. use cast
        1. float(3)/2 => 1.5
        2. 2/float(3) => 1.5
      3. importing from a module called future.
        This is a module in Python 2 that has Python 3 functions, this basically allows you to import Python 3 functions into Python 2

        1. from __future__ import division
        2. 3/2 => 1.5

String

In python3, the print function only support () format like print(“hello world”), doesn’t support inline like print “hello world”

>> s = 'hello world'

# show string length
>>> len(s)
11

# print a char of a specified position of the string ( the index is started by 0 )
>> print s[0]
h

>> print s[-1]
d

## note - we cannot use below way to change the particular char of the string
>>> s[0] = 100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
##

# print a char range of the string, the default left value is 0 and right value is -1
>> print s[1:]
ello world

>> print s[:3]
hel

>> print s[0:5]
hello

>> print s[:-1]
hello worl

# print char of the string by step
# same as for(i=0; i<len(str) i=i+step)
>> print s[::2]
hlowrd

>> print s[::3]
hlwl

# step also can be inverse
>> print s[::-1]
dlrow olleh

# concate
>>> s + ' man'
'hello world man'

>>> s = s + ' man'
>>> s
'hello world man'

# print multiple times
>>> s * 2
'hello world manhello world man'

# string functions
>>> s.upper()
'HELLO WORLD MAN'

# like php exploe
>>> s.split(' ')
['hello', 'world', 'man']

Comment


# this is single line comment , it can be used in behind of python code as below

print("hello world!") # comment is here

'''

this is comment block

'''

 

we cannot use /* */ as comment in python.

 

Programming
[Python3] installation – tarball

python packages: https://www.python.org/ftp/python/ I tried install Python-3.6.4  tarball on RHEL6 and RHEL7, looks no problem. Install pre-required packages $ sudo yum install openssl openssl-devel -y Install python3 by tarball $ wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz $ tar -xJf Python-3.6.4.tar.xz $ cd Python-3.6.4 ############## # enable ssl module ############## $ vi Modules/Setup  <= un-comment the following lines # …

Programming
[Python] Print Formatting

>>> name = ‘Lego Chen’ >>> job = ‘engineer’ >>> print ‘%s is an %s’ %(name, job) Lego Chen is an engineer >>> print ‘%10s is an %20s’ %(name, job) Lego Chen is an engineer >>> print ‘The flotting number is: %10.3f’ %(1000.33356) The flotting number is: 1000.334 >>> print …

jupyter
[Python] Jupyter Server Setup

Purpose: I’d like to setup a jupyter sever that only can be edited by me and I also can share the jupyter notebooks to anyone. There are two URLs, one for admin (me) and another for share to anyone. Admin: https://jupyter-admin.chenlego.me Share(guest): https://jupyter.chenlego.me But, current jupyter server doesn’t support permission management for …