Basics Of Python Language
Elijah Macharia
Posted on February 11, 2023
INTRODUCTION TO PYTHON PROGRAMMING
Python is a high-level-programming language. By high-level-programming language, it means that python is easy to understand. It has been used over many decades and it is considered to be the best and also easy to understand language as compared to other languages. It is also used by big tech companies and also small and medium-sized companies to perform this such as:
Web development
Software Development
Mathematics
System Scripting
Python Install
Windows Install
Maneuver to your windows and search for Command Prompt and press enter. Later type this code python --version
and see if you have python installed in your system.
Linux and Mac Install
In both Linux and Mac operating system, Python is already installed directly.
Python Syntax
Syntax simply refers to a set of instructions that a computer is given to generate an output. Failure to do so your program may run in errors instead. It is advisable to follow them so that you do not spend more time than expected on your code.
1.Indentation
Indentation refers to putting the a appropriate 'punctuation marks' so that your code does not run in errors.
Example
The correct syntax for indenting an if...else statement in python is;
a = 10
b = 22
if a > b:
print(" This is Correct")
else:
print("This is Incorrect")
Note:
The addition of
:
in the code after declaring it, it must be there, or else you may run into an error called a syntax error.The spacing before the print statement should be there in order for us not to get a syntax error. Any spacing > 1 is accepted.
The 'p' in
print()
is in small letters and not in capital so that it doesn't display a syntax error.
2.Comments
Comments are put using the #
symbol.
Comments in coding are used for;
- Make the code more readable.
- Are used to prevent execution when texting code.
- Used to explain python code.
There are two types of comments;
1.Single-Line comments
Example:
#Addition
a = 2
b = 3
print("2+3")
Output:
5
Note:
- The
#addition
has been put at the beginning of the code, the code will run without the comments. - Comments can be kept in any part of the code.
2. Multi-line comments
Multi line comments are used to display more than one line of comment instead of using the #
symbol everywhere in your comments.
Multi-line comments are represented using the """code"""
.
Example:
"""
Hello there!
This is how to run 'python is cool' in python.
"""
print("Python is cool")
Note Multi-line comments can be displayed if they are assigned values before them and printed.
Example
value = """This multi-line comment will be printed."""
print(value)
Python Variables
Variables
Variables are assigned containers for storing data values.
By the word 'assigned containers' it means that something must be put in front of it followed by an =
sign.
Example
x = 2
In this example the value of x
here has been assigned the value of 2
. This is an example of a container.
The word data values is use to describe if the value is either a string str
, integer int
, boolean bool
character char
or a double float
.
A variable is created when you assign a value to it x = 2
.
In this case there are rules in which we must follow when giving our variable names. These are;
- A variable name must start with a letter or the underscore character.e.g
_my_var = "pets"
- A variable name cannot start with a number. e.g.
2my_var = "family"
- A variable name can only contain underscores (A-z, 0-9 and _ ).e.g.
My_Var2 = "students"
- Variable names are case-sensitive.e.g
age, Age and AGE
are 3 different variables.
There are some techniques that one can use to make them more readable. They include;
1. camelCase
Each word, except the first, starts with a capital letter
Example
myVariableName = "children"
2. PascalCase
Each word starts with a capital letter.
Example
MyVariableName = "family"
3. snake_Case
Each word is separated by an underscore character.
Example
my_Variable_Name = "pets"
Posted on February 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024