Creation and use of strings

--Originally published at Start in the world of the #TC101

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. Creating strings is as simple as assigning a value to a variable.


Accessing Values in Strings

Python does not support a character type; these are treated as strings of length one, thus also considered a substring.

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.


Updating Strings

You can “update” an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether.


Source: https://www.tutorialspoint.com/python/python_strings.htm


Use of “else” with a conditional (and elif for Python)

--Originally published at Start in the world of the #TC101

It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false, if else.

flowchart_if_else.png

 

if BOOLEAN EXPRESSION:
    STATEMENTS_1        # executed if condition evaluates to True
else:
    STATEMENTS_2       # executed if condition evaluates to False

Example:


Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional

flowchart_chained_conditional.png

Example:

Source: http://www.openbookproject.net/books/bpp4awd/ch04.html


Use of the conditional “if”

--Originally published at Start in the world of the #TC101

 

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly.

A few important things to note about if statements:

  1. The colon (:) is significant and required. It separates the header of the compound statementfrom the body.
  2. The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  3. All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.

Example:


Nesting of conditional statements

--Originally published at Py(t)hon

You may think there is no more you must know of the conditional statement, well… You are wrong, there is another knowledge call nesting that must master. Perhaps you didn’t know that in a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.

There may be a situation when you want to check for another condition after a condition resolves to true, this is what is call nesting, a conditional inside another conditional.

Here is an example:

nesting-1

Here is a youtube video:

That’s all #Pug #Nesting #If #Python #ISC #Tec #TC101

 


Else and Elif

--Originally published at Py(t)hon

Continuing with the course, we have the next step that is the else and elif statement. Let’s start with else, in the else statement is where you are going to put the block of code that you want to execute if the conditional in the if statement turns out to be False or 0.

Here is an example:

else-1

On the other hand, we have the elif expression, that allows you to check for multiples condition for True and as soon as one of then turns out to be True, run.

Here is an example:

elif-1

Here is a video, for the people than didn’t understand me:

#Tec#Python#Else#Elif#ISC#TC101#Pug


If you click on this post…

--Originally published at Py(t)hon

This time we are going to see   the conditional if and how does it work. The conditionals statement performed different actions depending on how the boolean was evaluated false or true. The general form of the “if” statement is the follow.

If BOOLEAN :

STATEMENTS

There are some important things to remember about the if statement when use:

  • The colon (:) is required. It separates the header from the body.
  • The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
  • All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.

Here is an example:

if-1

Here is a tutorial if you didn’t get it:

That’s all #Pug#Tec#ISC#If#Python#Basic


Calling functions

--Originally published at Start in the world of the #TC101

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name. We have already seen one example of a function call:

>>> type(32)
<type 'int'>

The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.

It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value