python foundation (py3.6 installation, annotations, input and output, variables, data types, arithmetic symbols)

python installation

python3:
1. Get the source package (3.6) - > Download it on the official website (www.python.org)
2. tar zxf Python-3.6.4. tgz-C/opt/# decompression installation package
3. Enter the decompressed directory to compile and install:
Yum install GCC zlib zlib-devel openssl-devel-y # Resolves dependencies
4.cd/opt/cd Python-3.6.4/ Enter the decompressed installation package for compilation
./configure --prefix=/usr/local/python3 --with-ssl
prefix: Installation path -- with-ssl: add SSL encryption
Make & & make install: Installation
5. Testing: cd/usr/local/python 3/bin. /python 3
6. Add Python 3 commands to environment variables
Method 1:
Temporarily add:

export PATH="/usr/local/python3/bin:$PATH"
python3
Python 3.6.4 (default, Aug 26 2019, 21:44:00) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Permanently add:

echo export PATH="/usr/local/python3/bin:$PATH" >> ~/.bashrc
vim ~/.bashrc 
source ~/.bashrc 
python3
Python 3.6.4 (default, Aug 26 2019, 21:44:00) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>> 

Method 2:
Make Soft Connection
ln -s /usr/local/python3.6/bin/python3.6 /usr/local/bin/
## Test for successful installation
python3
## Install ipython

cd /usr/local/python3.6/bin
pip3.6 install ipython

The first py command

#coding:utf-8
# Python 2.x: ASCII encoding is used by default
# Python 3.x: UTF-8 encoding is used by default
# 1. No semicolon (Coding Specification PEP8)
# 2. Strictly follow the indented language

print('White tea has nothing to do with happiness.')
print('More Efforts')


## Notes
# One-line comment
"""
xxxxx multi-line annotations
xxxx
"""

Input and output

##python3.x
input(): Receives any data type
There is no raw_input() in Python 3.x

 >>> input('Num:')
 Num:2
 '2'
 >>> input('Num:')
 Num:abc
 'abc'
 >>> input('Passwd:')
 Passwd:123
 '123'
#Input content does not echo
 >>> import getpass
 >>> num = getpass.getpass('Please input a password:')
 //Please input a password:
 >>> print(num)
 123

##python2.x
input(): Only the correct numerical type is supported
raw_input(): Numbers and strings

>>> input('Num:')
 Num:2
 2
 >>> input('Num:')
 Num:1.2
 1.2
 >>> input('Num:')
 Num:redhat
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<string>", line 1, in <module>
 NameError: name 'redhat' is not defined
 >>> raw_input('Num:')
 Num:2
 '2'
 >>> raw_input('Num:')
 Num:1,2
 '1,2'
 >>> raw_input('Num:')
 Num:redhat
 'redhat'
 >>>

# If the received values are to be compared, they must be converted to the same type.

 >>> age = input('age:')
 age:19
 >>> age
 '19'
 >>> age > 18
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: '>' not supported between instances of 'str' and 'int'
 >>> age = int(age)
 >>> age
 19
 >>> age > 18
 True
 >>> age = int(input('age:'))
 age:18
 >>> age
 18

Format output

Format output
% s: Represents string% d: integer

 >>> name = 'westos'
  >>> name
  'westos'
  >>> age = 12
  >>> print('%s Age is%d' %(name,age))
  westos The age is 12.
  >>> age = 18
  >>> print('%s Age is%d' %(name,age))
  westos The age is 18.
  >>> age = '19'
  >>> print('%s Age is%d' %(name,age))
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: %d format: a number is required, not str
  >>> age = 19.5
  >>> print('%s Age is%d' %(name,age))
  westos Age 19

Floating point type%f

 >>> money=23121312.32314432
  >>> name = 'Tom'
  >>> print('%s Wages are%f' %(name,money))
  Tom The salary is 2321312.323144
  >>> money=60000
  >>> print('%s Wages are%f' %(name,money))
  Tom The salary is 60,000..000000
  >>> money=60000.99
  >>> print('%s Wages are%f' %(name,money))
  Tom The salary is 60,000..990000
  >>> print('%s Wages are%.2f' %(name,money))
  Tom The salary is 60,000..99
  >>> money=60000
  >>> print('%s Wages are%.3f' %(name,money))
  Tom The salary is 60,000..000

Integer placement: insufficient digits preceded by 0

>>> sid = 1
  >>> name = 'lily'
  >>> print('%s The school number is 000.%d' %(name,sid))
  lily The school number is 0001.
  >>> sid = 2
  >>> print('%s The school number is 000.%d' %(name,sid))
  lily The school number is 0002.
  >>> sid = 10
  >>> print('%s The school number is 000.%d' %(name,sid))
  lily The school number is 00010.
  >>> print('%s The number of the student is ____________%.5d' %(name,sid))
  lily The school number is 00010.
  >>> sid = 1
  >>> print('%s The number of the student is ____________%.5d' %(name,sid))
  lily The school number is 00001.
  >>> sid = 20
  >>> sid = 100
  >>> print('%s The number of the student is ____________%.5d' %(name,sid))
  lily The school number is 00100.

Realization of Percentage Sign

>>> scale = 0.1
  >>> print('The ratio of data is:%.2f' %(scale))
  //The ratio of data is 0.10
  >>> print('The ratio of data is:%.2f' %(scale * 100))
  //The ratio of data is: 10.00
  >>> print('The ratio of data is:%.2f%' %(scale * 100))
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  ValueError: incomplete format
  >>> print('The ratio of data is:%.2f%%' %(scale * 100))
  //The ratio of data is 10.00%.

variable

# Hump Command Law:
1. Big Hump: Every word has its initials capitalized
FistName LastName
2. Hump: The first word begins with lowercase and the next word begins with capitalization.
fistName lastName
# str: Represents a string type

   name = 'The landlord's meow'
   print(name)

# int: Represents an integer

   age = 22
   print(age)

# float: Represents a floating-point type

   height = 178.5
   print(height)

# bool: Represents a Boolean type with only True and False values

   gender = True
   print(gender)

# A variable name is defined only when it first appears.

data type

integer

>>> a = 1
>>> print(a)
1

View the types of variables

>>> type(a)
<class 'int'>

float

>>> b = 1.2
>>> print(b)
1.2
>>> type(b)
<class 'float'>
>>> c = westos
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'westos' is not defined

String type

>>> c = 'westos'
>>> print(c)
westos
>>> c = "what's"
>>> print(c)
what's
>>> c = 'what's'
  File "<stdin>", line 1
    c = 'what's'
              ^
SyntaxError: invalid syntax
>>> c = 'what\'s'
>>> print(c)
what's

bool type (only two values: True False is true if it is not zero)

>>> a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> bool('')
False
>>> bool(' ')
True
>>> bool('redhat')
True

Conversion between data types

>>> a = 1
>>> type(a)
<class 'int'>
>>> float(a)
1.0
>>> type(a)
<class 'int'>
>>> b = float(a)
>>> b
1.0
>>> b = 2.0
>>> int(b)
2
>>> c = 'redhat'
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'redhat'
>>> b = 123
>>> str(b)
'123'
>>> c = '123'
>>> int(c)
123
>>> a
1
>>> b
123
>>> c
'123'

Delete a variable from memory

>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> del b
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

Arithmetic operator

python2.x

>>> 5/2
2
>>> 100/300
0
>>> 5.0/2
2.5
>>> 100/300.0
0.3333333333333333

The division in Python 2.x requires that one of them be written as a floating-point type, otherwise it can be rectified by itself.

python3.x

>>> 5/2
2.5
>>> 100/300
0.3333333333333333
>>> 

Remainder

>>> 5%2
1

Rounding

>>> 5//2
2

>>> a = 1
>>> a = a+1
>>> a
2
>>> a += 1
>>> a
3

Logical Operator

"""
and
1 and 2
Return to True if both conditions are satisfied at the same time
As long as one condition is not satisfied, return to False

or
1 or 2
Return True if only one of the two conditions is satisfied
If both conditions are not met, return to False
"""

python_score = 60
c_score = 50

if python_score >= 60 and c_score >=60:
    print('Pass the exam')
else:
    print('Failure to pass the exam')

Keywords: Python SSL zlib Red Hat

Added by DeathStar on Mon, 02 Sep 2019 07:41:39 +0300