Skip to main content

Python Basics

Python Basics

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 Basics

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: 

Python Basics

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 Basics

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:

Python Basics

Setting aside the fact that data-type can be set, data-types can also be specified. For example
x = list(("apple", "banana", "cherry"))

#display x:
print(x)

#display the data type of x:
print(type(x)) 

Output:

Python Basics

Python numbers

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: 

Python Basics
Float:

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:

Python Basics
Complex: 

All complex number in python have the j component which is the imaginary part

z = -165j
print(type(z))

Output:

Python Basics
Note: Python numbers can be converted from one type to another.

 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:

Python Basics
Float () - create a number from an integer, a float or a string (provided the string is an integer)
y = float(2.8)
print(y)

Output:

Python Basics
str( ) – create a string from a group of data types, including strings, integer and a float 

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...