Friday, November 10, 2017

Python operators and signs at Codelent


Python operators and signs----
List of operators , signs and their uses---

Operator
Operation
Example
Evaluates to...


**

Exponent

2 ** 3

8



%

Modulus/remainder

22 % 8

6



//

Integer division/floored quotient

22 // 8

2



/

Division

22 / 8

2.75



*

Multiplication

3 * 5

15



-

Subtraction

5 - 2

3



+

Addition

2 + 2

4


Signs                                 name                         uses
                               single quote                       ‘aa’ or ‘hi my name is George’ or   ’34’ or   ’30-9’
                              double quote                    “good” or “I want to program” or  “ 89 is a” or “50+34”
“””                          triple quote                        it is use for string that is more than one line. Example
                                                                                “”” my name is George Ebili .
                                                                                I am from Ebonyi Abakaliki.
                                                                                I took programming as my hobby”””
#                             comment                   anything written after a comment sign is ignored by python.
                                                                                Example
                                                                   # This program says hello and asks for my name.
 Python ignores comments, and you can use them to write notes or remind yourself what the code is trying to do. Any text for the rest of the line following a hash mark (#) is part of a comment.

,                               comma                              34,55,6,7 or “do”,”u”, “hello” or ‘go’,’come’, ‘I w be’ 

()                             bracket                                 (‘hello’)  or  print(“good day brothers”)

{}                             dict bracket                        {'help':['root','can'],'num': [6,7,8]}

=                             equal sign               it is use to store some ideal a string, number, function or class
                                                                                It is also use in Boolean expression. Example
                                                                                A =  ‘help’,’run’,”go”,60

[]                             sort bracket                        [67,78,89,78] or [‘rat’,50,90,’hello’]

<                             less than                              1<4 or 30<50

>                             greater than                       90>60 or 100>10

!=                            not true                               40 != 30

This are the common operators used for now.
Android users should always add "print"  while testing the codes.

The Integer, Floating-Point, and String Data Types
Remember that expressions are just values combined with operators, and they always evaluate down to a single value. A data type is a category for values, and every value belongs to exactly one data type. The most common data types in Python are listed in Table 1-2. The values -2 and 30, for example, are said to be integer values. The integer (or int) data type indicates values that are whole numbers. Numbers with a decimal point, such as 3.14, are called floating-point numbers (or floats). Note that even though the value 42 is an integer, the value 42.0 would be a floating-point number.
Table 1-2. Common Data Types

Data type
Examples

Integers

-2, -1, 0, 1, 2, 3, 4, 5


Floating-point numbers

-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25


Strings

'a', 'aa', 'aaa', 'Hello!', '11 cats'

Python programs can also have text values called strings, or strs (pronounced “stirs”). Always surround your string in single quote (') characters (as in 'Hello' or 'Goodbye cruel world!') so Python knows where the string begins and ends. You can even have a string with no characters in it, '', called a blank string.
String Concatenation and Replication
The meaning of an operator may change based on the data types of the values next to it. For example, + is the addition operator when it operates on two integers or floating-point values. However, when + is used on two string values, it joins the strings as the string concatenation operator. Enter the following into the interactive shell:
>>> 'Alice' + 'Bob'
'AliceBob'
The expression evaluates down to a single, new string value that combines the text of the two strings. However, if you try to use the + operator on a string and an integer value, Python will not know how to handle this, and it will display an error message.
>>> 'Alice' + 42
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    'Alice' + 42
TypeError: Can't convert 'int' object to str implicitly
The error message Can't convert 'int' object to str implicitly means that Python thought you were trying to concatenate an integer to the string 'Alice'. Your code will have to explicitly convert the integer to a string, because Python cannot do this automatically. (Converting data types will be explained in Dissecting Your Program when talking about the str(), int(), and float() functions.)
The * operator is used for multiplication when it operates on two integer or floating-point values. But when the * operator is used on one string value and one integer value, it becomes the string replication operator. Enter a string multiplied by a number into the interactive shell to see this in action.
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
The expression evaluates down to a single string value that repeats the original a number of times equal to the integer value. String replication is a useful trick, but it’s not used as often as string concatenation.
The * operator can be used with only two numeric values (for multiplication) or one string value and one integer value (for string replication). Otherwise, Python will just display an error message.
>>> 'Alice' * 'Bob'
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    'Alice' * 'Bob'
TypeError: can't multiply sequence by non-int of type 'str'
>>> 'Alice' * 5.0
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    'Alice' * 5.0
TypeError: can't multiply sequence by non-int of type 'float'
It makes sense that Python wouldn’t understand these expressions: You can’t multiply two words, and it’s hard to replicate an arbitrary string a fractional number of times.

Our next chapter will be discussing on
(1)   Variables
(2)   Assignment  statement
(3)   Variable name
(4)   Your first program
(5)   Keyword and functions
You can also join our social media groups for more updates.

2 comments:

  1. I can't understand how does the tripple coat, work.
    Coz d seem to work same

    ReplyDelete
  2. yes you are write but triple quote are used from multiple lines.
    example """hello my name is george and i deal with python coding, i want to inform you that your system is about to go down in a minute from now"""

    now if you use a double quote to string this sentence you ill get an error message. because the string is written in more than one line.

    ReplyDelete