Sunday, September 29, 2019

Python tutorial 6 | if statement

This is the sixth of Python Tutorials for Beginners. You can see the other Python tutorials in this blog here. This Python beginner tutorial explains the if statement in Python with multiple examples. You can use the if statement to run a block of code depending on a condition. You can write a Python condition using Python comparison operators or Python logical operators. Please view the Python video tutorial 6 or read on...

In Python programming language, the block of code is formed by indenting it by a few spaces, typically 4 spaces. The if statements in Python programming have three formats, the if statement, the if else statement and the if elif else statement. Now let us see these formats with Python examples.

Note : the formats are followed by Python samples

In the first format, the condition is evaluated. If the condition is True, then the code block is run by Python. If the condition is False, nothing happens.

if condition:
    code block

# Python code example
# input function request a string from the user
name = input('Enter a name : ')
# len function returns the length of the string, name
if len(name) > 5:
    print(name, ' has the length', len(name))

Sunday, September 22, 2019

Python tutorial 5 | Logical Operators

This is the fifth of Python Tutorials for Beginners. You can see the other Python tutorials in this blog here. This Python beginner tutorial explains logical operations in Python with examples. It also shows operator precedence within logical operators and between Python comparison operators and Python logical operators. Please view the Python video tutorial 5 or read on...

Python programming language has three logical operators. The logical operators in Python are not operator, and operator and or operator. Now, there are two Boolean values, which are True and False.

The not operator results in the other Boolean value. If a condition is True, the not operator converts it to False. If the condition is False, the not operator converts it to True. The and operator needs two conditions. If both the conditions are True, the and operator gives the result as True. Else, the and operator gives the result as False. The or operator also needs two conditions. If both the conditions are False, the or operator gives the result as False. Else, the or operator gives the result as True. If a logical expression has multiple operators, the Python logical operators have different operator precedence. The not operator has the highest priority, then the and operator and finally, the or operator.

Now, let us see these logical operators with examples.

Sunday, September 15, 2019

Python tutorial 4 | Comparison Operators

This is the fourth of Python Tutorials for Beginners. You can see all the Python tutorials in this blog here. This Python beginner tutorial explains comparison operations in Python programming with multiple examples. Please view my Python video tutorial 4 or read on...

You can use Python comparison operators when you write conditions in Python if statement or Python while loop statement. In Python programming language, the comparison operators include equal to, not equal to, less than, less than or equal to, greater than and greater than or equal to. The result of any Python comparison operation is a True or False value.

A single Python condition may contain different comparison operators but all comparison operators in Python have the same priority. This means that there is no operator precedence.
Now, let us see these comparison operators with examples.

Sunday, September 8, 2019

Python tutorial 3 | Arithmetic Operators

This is the third of Python Tutorials for Beginners. In order to get started, you can see Python Tutorial 1 | Introduction and Python tutorial 2 | Handling Strings. Let us learn about arithmetic operators in Python programming language. This tutorial explains the arithmetic operators in Python programming. Please view my Python video tutorial 3 or read on...

What is an operator? It is a construct for processing one or more data values. In Python programming language, an operator is shown by a special character or a keyword.

There are several arithmetic operators in Python. These are + for addition, - for subtraction, * for multiplication, / for division, // for floor division or integer division, % for remainder of integer division and ** for power. Now, let us see examples of integer add, float add, integer subtract, float subtract, integer multiply, float multiply, division, floor division, remainder and power arithmetic operations in Python.

Sunday, September 1, 2019

Python tutorial 2 | Handling Strings

This is the second of Python Tutorials for Beginners. In order to get started, please first see the Python Tutorial 1 | Introduction. Let us learn how to handle strings in Python programming language. Please view my Python video tutorial 2 or read on...

A string is a sequence of characters. A string is enclosed by single quotes or double quotes. A multi-line string is enclosed by triple quotes (either both single quotes or both double quotes). You can see your string by using the print function. The print function omits enclosing quotes. The + Python string operator concatenates (combines) strings. The * Python string operator repeats a string a number of times. You can assign multiple variables in a single Python statement. Using indexing, you can extract a single character from a string. Using slicing, you can extract any part of a string. Strings in Python programming language are immutable. This means that once you define a string, you cannot change it. Now, let us see examples of these concepts.