Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small- and large-scale projects. Python is named after Monty Python and its famous flying circus, not the snake.
Where is python used?
Python is applied in:
developing websites.
software development.
task automation.
data analysis and data visualization.
Machine Learning and Artificial Intelligence
Installation
https://www.python.org/downloads/
use the link to download the latest version of python.
Your first python program
There is a tradition that the first program you ever run in any language
generates the output “Hello, world!”
We type “print” followed by an opening round brackets and the text
“Hello, world!” surrounded by single quotes, ending with a closing round bracket.Press enter to move to the next line
print('Hello, world!')
The following is outputed.
Hello, world!
Note that Python, as with many but not all programming languages, is “case
sensitive”. The word “print” is not the same as “Print” or “PRINT”.
Comments
A comment in Python starts with the hash character, # , and extends to the end of the physical line. It is also possible to use Triple Quotation (''') for multiline comments.
Variables
A variable is reserved memory location (containers) to store values.Variables do not need to be declared with any particular type, and can even change type after they have been set.A variable is created the moment you first assign a value to it.
It must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
keywords are not to be used as variable names
Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers.
Literals
Literals in Python is defined as the raw data assigned to variables or constants while programming. We mainly have five types of literals which includes string literals, numeric literals, boolean literals, literal collections and a special literal None.
Data types
Variables can store data of different types.
Different types can do different things.
- Text Type: str
- Numeric Types: int, float, complex
- Sequence Types: list, tuple, range
- Mapping Type: dict
- Set Types: set, frozenset
- Boolean Type: bool
- Binary Types: bytes, bytearray.
Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
print(46 + 2) # 48 (addition)
print(10 - 9) # 1 (subtraction)
print(3 * 3) # 9 (multiplication)
print(84 / 2) # 42.0 (division)
print(2 ** 8) # 256 (exponent)
print(11 % 2) # 1 (remainder of the division)
print(11 // 2) # 5
Comparison operators
Comparison operators are used to compare values. It returns either True or False according to the condition.
**Comparison operators**
Comparison operators are used to compare values. It returns either True or False according to the condition.
x = 10
y = 12
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
Logical operators
Logical operators are the and, or, not operators.
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Other operators include:
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
Assignment operators are used in Python to assign values to variables.
Conditions
Includes:
if-else
elif
nested if
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
Loops
A Loop is a control flow statement that is used to repeatedly execute a group of statements as long as the condition is satisfied.
Include:
- while loop. With the while loop we can execute a set of statements as long as a condition is true
while condition:
statements(code)
for loop.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.nested loops.
Nested loops mean using a loop inside another loop. We can use any type of loop inside any type of loop
even_list = []
for item in range(1, 11):
while item % 2 == 0:
even_list.append(item)
break
print("Even Numbers: ", even_list)
Functions
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a Function
In Python a function is defined using the def keyword:eg
def my_function():
print("Hello from a function")
Information can be passed into functions as arguments.
Calling a function
Once we have defined a function, we can call it from another function, program, or even the Python prompt. To call a function we simply type the function name with appropriate parameters.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
greet('Paul')
And congratulations!
You have completed an introductory course on Python. Well done.
It is only an introductory course and there is more. But do not let that
dishearten you; just take a look at what you have accomplish.
Top comments (0)