Tuesday, May 19, 2020

Floats


When we consider the floating-point numbers,

Example:  2.5 , 0.4

In above example 2.5 looks normal when we write it in a program, although if our native language prefers to use a comma instead of a point in the number, we should ensure that the number doesn’t contain any commas at all.

Python will not accept that or may misunderstand the intentions, as the comma itself has its own reserved meaning in Python.

If we want to use just a value of two and a half, we should write it as shown above. We should carefully notice about this, there is a point between 2 and 5 but, not a comma.




But when writing zero point four can be written in Python, we can omit zero when it is the only digit in front of or after the decimal point.

So, we can write 0.4 as .4 and 4.0 can write as 4.

This will change neither its type nor its value.

Integers vs floats

If we take these two numbers 4 and 4.0


We may think that they are exactly the same, but Python sees them in a completely different way.
4 is an integer number and 4.0 is a floating-point number.

Also if we want to write very large numbers we can use scientific notation.

When we write the speed of light 3 x 108 , in Python it can achieved through the following way.

The letter E (or lower case letter e – it comes from the word exponent) is a concise record of the phrase times ten to the power of.

  1. The exponent (the value after the E) has to be an integer.
  2. The base (the value in front of the E) may be an integer.


Coding Floats

When we write 6.62607 x 10-34 in Python,




But sometimes Python may choose different notations.





Python always chooses the more economical form of the number’s presentation and we should take this into consideration when creating literals.

Monday, May 18, 2020

Integers in Python


When we writing a number in Python, it doesn’t accept things like these such as 11,111,111 or 11.111.111 or 11 111 111.
So, in Python we can write this number like 11111111 or 11_111_111.

Python 3.6 has introduced underscores in numeric literals, allowing for placing single underscores between digits and after base specifiers for improved readability. This feature is not available in older versions of Python.

Also in Python, we can write negative numbers by adding minus mark (-) to the front of the number like these -11111111 or -11_111_111.
Positive numbers don’t need to be preceded by the plus sign (+). But it’s permissible, if we wish to do it. These numbers describes the same number +11111111 and 11111111.

Octal Representation

If an integer number is preceded by a 0O or 0o prefix (zero-o), it will be treated as an octal value. This means that the number must contain digits taken from the [0..7] range only.

0o123 is an octal number with a (decimal) value equal to 83.

The print() function does the conversion automatically.


Hexadecimal Representation


If an integer number is preceded by a 0X or 0x prefix (zero-x), it will be treated as a hexadecimal value.

0x123 is a hexadecimal number with a (decimal) value equal to 291.

The print() function does the conversion automatically.


Wednesday, May 13, 2020

The print () function – Part 2


The print () function – Using Multiple Arguments

Now we are going to try this function with more than one argument. Here the arguments are separated by commas.
Look at the below example.



We can gain two conclusions from the above example.

  • This function invoked with more than one argument outputs them all on one line.
  • This function puts a space between the outputted arguments on its own initiative.


The print () function – The Keyword Arguments

The mechanism is called keyword arguments. The name stems from the fact that the meaning of these arguments is taken not from its location (position) but from the special word (keyword) used to identify them.

The print() function has two keyword arguments that you can use for your purposes. The first of them is named end.
In order to use it, it is necessary to know some rules:

  • a keyword argument consists of three elements: a keyword identifying the argument (end here) an equal sign (=) and a value assigned to that argument
  • any keyword arguments have to be put after the last positional argument (this is very important)

print("My name is","Python.",end="")
print("Monty Python.")

Output - 
My name is Python. Monty Python.


The default behavior reflects the situation where the end keyword argument is implicitly used in the following way: end ="\n"

print("My name is","Python.",end="\n")
print("Monty Python.")

Output - 
My name is Python.
Monty Python.

We've said previously that the print() function separates its outputted arguments with spaces. This behavior can be changed too.

The keyword argument that can do this is named sep (like separator).

The print() function now uses a dash, instead of a space to separate the outputted arguments.


print("My", "name", "is", "Monty", "Python.", sep="-")

Output - 
My-name-is-Monty-Python.



print("My","name","is",sep="_",end="*")
print("Monty","Python.",sep="*",end="*\n")
print("Hi")

Output - 
My_name_is*Monty*Python.
Hi


print("Programming","Essentials","in",sep="***",end="...")
print("Python")

Output - 
Programming***Essentials***in...Python

Tuesday, May 12, 2020

The print () function – Part 1


Before starting about this lesson I want to say a function may have two things. They are,

1.       An effect
2.       A result

Also the third point is important it is argument(s).

In Python, depending on the individual needs, they may accept any number of arguments as many as necessary to perform their tasks. So we should note here that, any number includes zero – some Python functions don’t need any argument.

print("Hello World!")

We can see the string is delimited with double quotes. That double quotes make the string. Almost anything we put inside the quotes will be taken literally, not as code, but as data.

This function,

1.       Takes it arguments.
2.       Converts them into human-readable form if needed
3.       Sends the resulting data to the output device

This function accept any type of arguments and not evaluate anything.

Python's syntax is quite specific in this area. Unlike most programming languages, Python requires that there cannot be more than one instruction in a line.

A line can be empty but it must not contain two, three or more instructions. This is strictly prohibited.

Note: Python makes one exception to this rule - it allows one instruction to spread across more than one line (which may be helpful when our code contains complex constructions).

The print () function with the escape and newline characters (\n)

Here both the backslash and the n form a special symbol named a newline character which urges the console to start a new output line.


If we want to put just one backslash inside a string, don't forget its escaping nature so, we have to double it.



Otherwise it will give an error.

Friday, May 8, 2020

Lists in Python

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.



Like strings lists can be indexed and sliced.



All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list.


Lists also support operations like concatenation.


Unlike strings, which are immutable, lists are a mutable type.



We can also add new items at the end of the list, by using the append() method.


Assignment to slices is also possible, and this can even change the size of the list or clear it entirely.


The built-in function len() also applies to lists.


It is possible to nest lists (create lists containing other lists).



Saturday, May 2, 2020

Strings in Python - Part 3

Strings can be indexed (sub-scripted) with the first character having index 0. There is no separate character type, a character is simply a string of size one.


Indices may also be negative numbers to start counting from the right.



Note that since -0 is the same as 0, negative indices start from -1.

In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain sub string.


Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s.

Slice indices have useful defaults, an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.



Extra Note:
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n.




The first row of numbers gives the position of the indices 0...6 in the string.

The second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.

For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds.

For example, the length of word[1:3] is 2.

Attempting to use an index that is too large will result in an error.



However, out of range slice indexes are handled gracefully when used for slicing.



Python strings cannot be changed because, they are immutable. Therefore, assigning to an indexed position in the string results in an error.



If you need a different string, you should create a new one.



The built-in function len ( ) returns the length of a string.


Friday, May 1, 2020

Strings in Python - Part 2


String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. Produces the following output (note that the initial newline is not included).





Strings can be concatenated (glued together) with the + operator, and repeated with *.


Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.



This only works with two literals though, not with variables or expressions.




If you want to concatenate variables or a variable and a literal, use +.




This feature is particularly useful when you want to break long strings.