title | sidebar_label |
---|---|
Python Basics |
Get started |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Python is a dynamically typed, interpreted language. Python interpreter is written in C-language. Interpreter program reads and performs the Python codes/ instructions. The interpreters interacts with the operating system layer (use network, keyboard, mouse, monitor, hard drive etc.).
It is customary to write our first program that prints some message in the screen: <Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
print("Hello Python!")
Hello Python!
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
a = 5
b = 3
a + b
8
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
a * b
15
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
a - b
2
:::tip Good to know
Due to how the numbers are stored in computer memory, floating point algebra sometimes might produce unexpected results. Notice the following discrepancy (floating point addition/ subtraction is not associative):
>>> (1.2 + 1E20) - 1E20
0.0
>>> 1.2 + (1E20 - 1E20)
1.2
:::
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
greeting = "Hello"
name = "Pranab"
print(greeting + " " + name)
Hello Pranab
For performance improvement and better readability, consider using join
instead of in-place string concatenation.
" ".join([greeting, name])
As we have seen, we do not need to define the datatype in python. Interestingly, for certain operations, we can even mix types: <Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
my_str = "Rain! "
my_str * 3 # multiplying a str with int
'Rain! Rain! Rain! '
Collecting user input: <Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
age = input("How old are you? ")
type(age)
How old are you? 34
<class 'str'>
:::note
Notice that variable assigned from input
is a string, even if a number is
entered.
:::
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
age = int(age)
type(age)
<class 'int'>
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
"Singapore".upper()
'SINGAPORE'
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
"Singapore".lower()
'singapore'
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
"das".capitalize()
'Das'
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
"pranab das".title()
'Pranab Das'
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
"ABCDefgh".swapcase()
'abcdEFGH'
<Tabs defaultValue="input" values={[ { label: 'Input', value: 'input', }, { label: 'Output', value: 'output', }, ] }>
name = 'Pranab'
age = 34
print("Name: {0}. Age: {1} years.".format(name, age))
Name: Pranab. Age: 34 years.
You can also format in the following way:
print("Name: {name}. Age: {age} years.".format(name=name, age=age))
Starting from Python 3.6 and above:
print(f"Name: {name}. Age: {age} years.")