Hi. Welcome back to another lesson on python. This lesson is the continuation of the introduction to python. If you are new here, please do check out our previous articles.
Python syntax
Python syntax arranges all the rules that are used to create sentences in python programming.
The two ways of executing syntaxes in python include writing directly into the command line which will be demonstrated below and creating a python file.py file extension (which is created when saving a file in the server)
print( "welcome to Trycoder" )
Output:
Python comments
The comments are being ignored by the compiler.
The symbol # indicates the start of command line and will continue till the end of the line as a comment.
print("Trycoder") #This is a comment.
Output:
Comments also prevent the compiler from reading a code.
Python variable
Variables in python are created when values are assigned to it.
Examples
x = 15
y = "KELVIN"
print(x)
print(y)
Output:
Python Data-type
Data type is very important in python programming. There are many data types in python with different variables. Only three will be mention in this tutorial, which are
- Text Type: str
- Number Types: int, float, complex
- Sequence Types: list, tuple, range and many more
Setting and getting data-type
Data type can easily be obtained by using the “type” function and set when a variable has a value assign to it.
An example of setting data type
x = ["mango", "watermelon", "grape"]
#display x:
print(x)
#display the data type of x:
print(type(x))
Output:
x = list(("apple", "banana", "cherry"))
#display x:
print(x)
#display the data type of x:
print(type(x))
Output:
They are three main types of python numbers. The variables created can be verified with the type function
Int:
Also known as an integer, is a positive whole number with unlimited length
r=12350455
print(type(r))
Output:
Also known as a floating point number, is a positive or negative number with a precision of one or two decimals.It can also be a scientific number with an e (exp 10)
S=12350.5
print(type(S))
Output:
All complex number in python have the j component which is the imaginary part
z = -165j
print(type(z))
Output:
Python has a default module known as “random” which can be used to make random numbers
Python casting
Casting is used to identify variables. Below, we will see how the constructor functions is used to verify variables
Int (): integers are being form from themselves, float (no decimal place) or a string (as long as the string is an integer)
x = int(4) # x will be 4
print(x)
Output:
y = float(2.8)
print(y)
Output:
Comments
Post a Comment
Please do not enter any spam link in the comment box.