MNPython
MNPython
What is python?
Python is a high-level, interpreted, and general-purpose dynamic
programming language known for its readability and versatility. It was
created in 1989 by Guido Van Rossum and has become one of the
most popular languages globally.
Syntax: the set of rules that define how a Python program should be
written and interpreted.
Variables:
Variables in Python are containers that store data values. Here are
the key points about variables in Python:
4. Naming Conventions:
- Variable names must start with a letter or an underscore (_)[1][2]
[4].
- Variable names can only contain letters, digits, and underscores[1]
[2][4].
- Variable names are case-sensitive[1][2][4].
- It is recommended to use lowercase letters and underscores to
separate words (snake_case)[2][4].
5. Printing Variables: You can display the value of a variable using the
print() function[5].
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
Good to know
The most recent major version of Python is Python 3, which we shall be
using in this tutorial. However, Python 2, although not being updated
with anything other than security updates, is still quite popular.
In this tutorial Python will be written in a text editor. It is possible to
write Python in an Integrated Development Environment, such as
Thonny, Pycharm, Netbeans or Eclipse which are particularly useful
when managing larger collections of Python files.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
Example:
if 5 > 2:
print("Five is greater than two!")
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Multiline Comments
Python does not really have a syntax for multiline comments.
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your
comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Variables
Variables are containers for storing data values.
Example:
x = 5
y = "John"
print(x)
print(y)
Casting
If you want to specify the data type of a variable, this can be done with
casting.
Variable Names
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). Rules for Python variables:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to
extract the values into variables. This is called unpacking.
Example: x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
You can also use the + operator to output multiple variables:
print(x + y + z)
Global Variables
Variables that are created outside of a function (as in all of the examples
above) are known as global variables.
Example:
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Variables can store data of different types, and different types can do
different things.
Python has the following data types built-in by default, in these categories:
You can get the data type of any object by using the type() function:
x = 5
print(type(x))
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
ADVERTISEMENT
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = range(6) range
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
Python Numbers
There are three numeric types in Python:
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example: x = 1 # int
y = 2.8 # float
z = 1j # complex
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(a))
print(type(b))
print(type(c))
Random Number
Python does not have a random() function to make a random number, but
Python has a built-in module called random that can be used to make random
numbers:
Example
Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
Strings
Strings in python are surrounded by either single quotation marks, or double
quotation marks.