What Is Python? Merit and Demerit
What Is Python? Merit and Demerit
What Is Python? Merit and Demerit
What is Python?
Merit and demerit.
• Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
• Python has provided us with a very complete basic code base, Including network, file, GUI, database,
text, etc., which make it called "batteries included ".Developed in Python, there are many functions
can be reused.
• In addition to the built-in libraries, Python has a large number of third-party libraries
• Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very
attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect
existing components together.
• Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program
maintenance.
• Python supports modules and packages, which encourages program modularity and code reuse.
• The Python interpreter and the extensive standard library are available in source or binary form without
charge for all major platforms, and can be freely distributed.
• Compared to C, Python runs slow.
C
Python
How to run:
1. Run code in Command window one by one.
2. Run .py file, which will execute command lines in batch.
Tips:
When developing program in Python, We can write code in a text editor, and open an
interactive command window. In the process of writing code, stick part of the code to the
command line to verify, which will make it more easily.
DATA TYPE
Python Keywords, Identifiers
Numeric Types
String
KEYWORDS, IDENTIFIERS
• Keywords are special words which are reserved and have a specific meaning. Python has a set of
keywords that cannot be used as variables in programs.
• All keywords in Python are case sensitive.
Python language lays down a set of rules for programmers to create meaningful identifiers.
1. To form an identifier, use a sequence of letters either in lowercase (a to z) or uppercase(A to Z).
However, you can also mix up digits (0 to 9) or an underscore (_) while writing an identifier.
2. You can’t use digits to begin an identifier name. It’ll lead to the syntax error.
3. Also, the Keywords are reserved, so you should not use them as identifiers.
4. Python Identifiers can also not have special characters [‘.’, ‘!’,‘@’, ‘#’, ‘$’, ‘%’] in their formation. These
symbols are forbidden.
5. Python doc says that you can have an identifier with unlimited length. But it is just the half truth. It will
lead to the violation of a rule set by the PEP-8 standard, which says “Limit all lines to a maximum of 79
characters.”
KEYWORDS, IDENTIFIERS
Have a try:
NUMERIC TYPES
Python supports four different numerical types −
•int (signed integers) − They are often called just integers or ints, are positive or negative whole
numbers with no decimal point.
•long (long integers ) − Also called longs, they are integers of unlimited size, written like integers
and followed by an uppercase or lowercase L.
•float (floating point real values) − Also called floats, they represent real numbers and are written
with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation,
with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
•complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j)
represents the square root of -1 (which is an imaginary number). The real part of the number is a, and
the imaginary part is b. Complex numbers are not used much in Python programming.
NUMERIC TYPES
Function Description
x+y Summation
x-y Subtraction
x*y Multiplication
x/y Division
x // y Floor Division
x%y Divides x by y and returns remainder
x ** y Performs exponential (power) calculation on operators
-x Negative x
+x No action
Function Description
abs(x) The absolute value of x: the (positive) distance between x and zero.
ceil(x) The ceiling of x: the smallest integer not less than x
cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y
exp(x) The exponential of x: ex
fabs(x) The absolute value of x.
floor(x) The floor of x: the largest integer not greater than x
log(x) The natural logarithm of x, for x> 0
log10(x) The base-10 logarithm of x for x> 0.
max(x1, x2,...) The largest of its arguments: the value closest to positive infinity
min(x1, x2,...) The smallest of its arguments: the value closest to negative infinity
How to convert?
bin(i), hex(i), int(i), int(s, base), oct(i)
STRING
• Strings are amongst the most popular types in Python. We can create them simply by enclosing
characters in quotes.
• Python treats single quotes the same as double quotes. But need to keep consistent.
• If you want to use single quotes in a string generated with double quotes, just do it. But if you
want to use double quotes in double quotes, you need to use escape character. Vice versa.
STRING
STRING
Slice and Step
S = “PYTHON!”
STRING
Slice and Step
s[start]
s[start : end]
s[start : end : step]
STRING
Slice and Step
s[: : 3] 'PH!'
s.center(width, char) ----Returns a space-padded string with the original string centered to a total of
width columns.
STRING
Function
find(t, start, end) ----Determine if str occurs in string or in a substring of string if starting index beg
and ending index end are given returns index if found and -1 otherwise.
replace(t, u, n) ----Replaces all occurrences of old in string with new or at most max occurrences if
max given.
STRING
Function
s.zfill(w) ----Return copy of s, if shorter than w, then fill with 0 from the start point.
split(t, n) ----Split string with t at most n times. If no maximum n set, split all.
STRING
Function
>>> s="Python" >>> s.isspace()
>>> s.islower() False
False >>> s.isalpha()
>>> s.isupper() True
False >>> s.isdigit()
>>> s.istitle() False
True >>> unicode(s, 'UTF-8').isnumeric()
False
STRING
String Concatenation
1. With “+”
>>> s="python"
>>> l="&learning"
>>> s+l
'python&learning’
2. With “join”
>>> list = ["aaa", "bbb", "ccc"]
>>> " ".join(list)
'aaa bbb ccc’
Tips: Symbol
STRING
s.format()
1. With input.
STRING
s.format()
2. With List.
3. With Dictionary.