Python is a programming language that lets you work quickly and integrate systems more effectively. www.python.org
- Easy to learn yet very powerful
- General purpose language that could be used almost everywhere
- Flexibility and scalability
- Multi-paradigm: object oriented, functional, procedural etc.
- Popular language, big community
- Fun to work with
- Career Opportunities
- CLI applications
- Web Apps backened / APIs
- GUI applications
- Network applications
- System Administration / DevOps
- Scientific Applications
- Data Science Projects
- Machine Learning / Deep learning
- and much more
There are two versions of python currently in use python 2.7 & python 3.
So, begineers often come up with question “Should I choose Python 2 or Python 3”?
- Most production applications today use Python 2.7.
- Python 3 is ready for the production deployment of applications today.
- Python 2.7 will only receive necessary security updates until 2020.
- The brand name “Python” encapsulates both Python 3 and Python 2.
Yes, start with python 3.x but familiarizing yourself with Python 2.7 will be very useful.
In case if you have to work with legacy projects in the future you might need to know 2.x too.
Python comes out of the box in most modern linux distros like Debian, Fedora, Centos etc. In case it's not preinstalled you can always download it from the official website.
You can download the installer from the official website. https://www.python.org/downloads/
You could use any code editors out there to write code in python as long as it supports syntax highlighting.
Here are some recommended editors/IDEs:
- Microsoft Vscode
- Atom
- Sublime
- Vim
- Jetbrains PyCharm IDE
Hello World in python is just a one liner.
print "Hello, World!"
Pretty neat. Isn't it?
In python3
it's
print("Hello, World!")
Mind the parenthesis. In python3 print
is a function.
Run python shell first.
➜ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Then
>>> print "Hello World!"
Hello World!
Create a file hello_world.py
with the code.
print "Hello World!"
Now run it with python.
➜ python hello_world.py
Hello World!
Python is a dynamic language, and running it from the command line essentially triggers the following steps:
1. The source is compiled the first time it is encountered (e.g., imported as a module or directly executed). This step generates the binary file, with a pyc or pyo extension depending on your system.
2. The interpreter reads the binary file and executes the instructions (opcodes) one at a time.
You need to be aware of few things in python.
- No semicolon
- No curly braces
- Indentation is a part of syntax
- Dynamically-typed language
Looks something like this
"""
Generates Fibonacci series upto n
"""
def fib(n):
a, b = 0, 1
while a < n:
print(a)
a, b = b, a+b
fib(1000)
# This prints Hello World!
print("Hello World!")
# This is a comment; single line comment
"""
And this is a multiline comment.
As this could take more than one line of comments.
Like this.
"""
print("Hello Again.")
Output:
Hello World!
Hello Again.
# Printing more texts
print("Hello World!")
print("Hello Again.")
print("We enjoy typing.")
print("Learning python is easy.")
print("And fun.")
print("Printing text is way easier.")
Output:
Hello World!
Hello Again.
We enjoy typing.
Learning python is easy.
And fun.
Printing text is way easier.
print("One + One =", 1 + 1)
print("One - Two =", 1 - 2)
print("Two * Five =", 1 * 5)
print("Four / Two =", 4 / 2)
print("Expression (12 * 5) - (2 ^ 3) + (1 / 2) =", ((12 * 5) - (2 ** 3) + (1 / 2)))
print("Seven is less than or equal to Six is", 7 <= 6)
Output:
One + One = 2
One - Two = -1
Two * Five = 5
Four / Two = 2
Expression (12 * 5) - (2 ^ 3) + (1 / 2) = 52
Seven is less than or equal to Six is False
first_name = "Kabir"
last_name = "Baidhya"
dob = "July 30, 1992"
home_town = "Kathmandu, Nepal"
print("Hi! I am", first_name, last_name, ".")
print("I was born on", dob, ".")
print("I'm from", home_town, ".")
Output:
Hi! I am Kabir Baidhya .
I was born on July 30, 1992 .
I'm from Kathmandu, Nepal .
first_name = "Kabir"
last_name = "Baidhya"
dob_month = "July"
dob_day = 30
dob_year = 1992
difficulty = "easy"
print("Hi! I am %s %s." % (first_name, last_name))
print("I was born on %s %d, %d." % (dob_month, dob_day, dob_year))
print("Python is %s to learn." % difficulty)
Output:
Hi! I am Kabir Baidhya.
I was born on July 30, 1992.
Python is easy to learn.
print("What is your name?")
# This would take input from the user
# and store it in a variable.
name = raw_input()
print("Hi! %s. \nIt's nice to meet you." % (name))
print("Hope you're doing good.")
Output:
What is your name? Kabir
Hi! Kabir.
It's nice to meet you.
Hope you're doing good.
- Numeric: int, float, long
- Boolean: bool
- Sequences: str, list, tuple, bytes
- Mappings: dict
- Sets: set, frozen set
Read more https://en.wikibooks.org/wiki/Python_Programming/Data_Types
- Immutable types
- int, float, long, tuple, byetes, frozen set, etc.
- Mutable types
- list, dict, set, etc.
an_integer = 6
a_floating_point = 17.60
a_boolean = True
a_string = "Foo"
print("Integer value = %d" % an_integer)
print("Float value = %f" % a_floating_point)
print("Boolean value = %r" % a_boolean)
print("String value = %s" % a_string)
Output:
Integer value = 6
Float value = 17.600000
Boolean value = True
String value = Foo
fruits = ['Banana', 'Apple', 'Lime']
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# This is called list comprehension
even_numbers = [x for x in numbers if x % 2 == 0]
print("Numbers: %s" % numbers)
print("Even Numbers: %s" % even_numbers)
print("Fruits: %s, %s and %s" % (fruits[0], fruits[1], fruits[2]))
Output:
Numbers: [1, 2, 3, 4, 5, 6, 7, 8]
Even Numbers: [2, 4, 6, 8]
Fruits: Banana, Apple and Lime
data = {
"name": "Kabir Baidhya",
"dob": "July 30, 1992",
"home_town": "Kathmandu, Nepal"
}
print("Hi! I am %s." % data["name"])
print("I was born on %s." % data["dob"])
print("I'm from %s." % data["home_town"])
Output:
Hi! I am Kabir Baidhya.
I was born on July 30, 1992.
I'm from Kathmandu, Nepal.
- http://docs.python-guide.org/en/latest
- https://www.python.org
- https://iluxonchik.github.io/why-you-should-learn-python/
- https://learnpythonthehardway.org/book/