Python Syntax and Data Types
After a long time, I’m back again! In this article, I will explain Python Syntax and Data Types.
Before starting, I want to mention where we can code? I will code on Python Jupyter Notebook.
OK, let’s start!
Python Syntax
Running Python Commands :
- Python commands can be run by typing directly on the Command-Line.
While many people learn python, they start with the first word Hello World”.
print("Hello World")
Output:
Hello World
- Or by creating a python file with .py extension on the server and running this file in Command-Line.
C: \Users\Your-Name>python myfile.py
Python Indents :
Python uses indentation to indicate a block of code.
For example,
print isn't under if the operator
if 5 > 2:
print("Five is greater than two!")
Output:
Five is greater than two!
If we do that, then it gives an error.
if 5 > 2:
print("Five is greater than two!")
Output:
Input In [4]
print("Five is greater than two!")
^
IndentationError: expected an indented block
Comments
Python has the ability to add comments within the code. Comments start with “#”.
#This is comment
print("Hello")
Output:
Hello
We can also three quotes.
'''
Hi everyone
My name's Python!'''
Output:
"\nHi everyone\nMy name's Python!\n\n"
Docstrings
It has an extended document capacity called Python document array. Docstrings can be one-line or multi-line. Python uses triple quotes at the beginning and end of the docstring.
Docstrings can also be used to write comments.
"""This is a multiline docstring."""
print("Hello, World!")
Output:
Hello, World!
Variables
The fields we create to temporarily store data in our Python programs are called variables. We can use single quotes or double quotes when making a verbal assignment (string) to variables. We should not use quotes when assigning a numeric assignment to variables. Otherwise, we will define a string variable.
variable_name = variable_value
name = "Celine"
age = 25
salary = 18000.50print(name)
print(age)
print(salary)
Output:
Celine
25
18000.5
Changing the value of Variables
I want to explain as a give a simple example.
For example,
#This variable is integer
variable = 30
print(variable)
Output:
30
We want to see this variable of type,
- with type command
variable = 30
print(variable)
Output:
30type(variable)
Output:
int
Now, we want to change to type. For example, string.
variable = "30"
print(variable)
type(variable)
Output:
30
str
Now, I want to explain data types. There are four types. These are,
- Int (Integer)
- Float
- String
- Boolean
Int:
I want to example for integer,
age = 40
type(age)
Output:
int
Float:
The float or decimal data type refers to positive or negative numbers that contain one or more decimals after the period.
I want to example float,
height = 1.80 #cm
type(height)
Output:
float
String:
Any string of characters is called a string in Python. It can be composed of a single letter, it can contain spaces, and it can contain special characters or numbers. In Python, the string data type is expressed with “str”.
game_name = "Sims 4"
type(game_name)
Output:
str
Boolean:
The Python Boolean type is one of Python’s built-in data types. It’s used to represent the true value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False . Understanding how Python Boolean values behave is important to programming well in Python.
For example,
a = 2
b = 5
a < b
Output:
Truetype(a < b)
Output:
boola = 8
b = 20
a == b
Output:
Falsetype(a == b)
Output:
bool
Multiple assignments in Python
Instead of assignment of variables one by one, we can do multiple assignments.
Let’s back to the above example that I give the variable.
name = "Celine"
age = 25
salary = 18000.50print(name)
print(age)
print(salary)
Output:
Celine
25
18000.5
How we can multiple assignments in this example?
It’s simple :)
name, age, salary = "Celine", 25, 18000.50
print(name)
print(age)
print(salary)
Output:
Celine
25
18000.5
Another example is,
We can write to value one by one.
number1 = 80
number2 = 90
number3 = 50
print(number1)
print(number2)
print(number3)
Output:
80
90
50
But if we want to do multiple assignments, then
number1, number2, number3 = 80, 90, 50
print(number1)
print(number2)
print(number3)
Output:
80
90
50
I hope while you’re reading my article you can enjoy it!
See you on another topic :)