No Fluff Guide to Python - Part 1
Python, Python, Python.
That's all I hear about.
Well, there's a reason. Python with its elegant syntax and versatility, has emerged as one of the most popular programming languages worldwide. Whether you're a beginner exploring the world of coding or an experienced programmer seeking a powerful tool, Python is the perfect language to start your journey. With a wide range of applications and a supportive community, Python provides an excellent introduction to the world of programming. In this blog post, we will dive into the exciting world of introductory Python programming, uncovering some intriguing facts along the way.
Fact #1: Python's Simplicity and Readability
One of the reasons why Python has gained immense popularity is its simplicity and readability. Its clean and concise syntax allows programmers to write code that is easily understood by humans. Python utilizes whitespace indentation to define code blocks, which encourages indentation practices and enhances code clarity. This makes Python an ideal language for beginners, allowing them to quickly grasp and write code without getting overwhelmed.
Fact #2: Python's Versatility
Another fascinating aspect of Python is its versatility. Whether you're interested in web development, data analysis, machine learning, or even game development, Python has you covered. Python offers numerous libraries and frameworks that cater to specific domains, simplifying complex tasks and saving valuable development time. Popular libraries like NumPy, Pandas, Matplotlib, and TensorFlow enable Python enthusiasts to unleash the power of data manipulation, visualization, and machine learning effortlessly.
Fact #3: Python's Massive Community and Resources
Python boasts a massive and supportive community that is always ready to assist and inspire new programmers. Online communities, forums, and open-source projects provide an abundance of resources, tutorials, and learning materials, making it easier than ever to get started with Python. The community's dedication to improving and expanding Python's capabilities contributes to its constant growth and evolution, ensuring that developers always have access to the latest tools and technologies.
Ok. Let's Go
Let's quickly setup python:
Step1: Download the software
Check out the latest version on (https://www.python.org/downloads/)
Step2: Run the software and Install now
Step 3: Get all the optional features, since we are going to take a look at all of them:
Step 4: Depending upon the version you installed, you should be able to find it in search
As a beginner, the easiest tool you can use along with python is VS Code. You can download it from here:
https://code.visualstudio.com/download
Let's jump into the actual coding:
>>> This is fun
You can save data in variables. These can then be used later to reference the same data:
The print command simply shows you what data is coming in through that variable.
Print command is very powerful and can do a lot of things. Let's see some examples:
Merge 2 variables and show the data together:
>>> Bugatti Chiron Super Sport 300+ costs around $27.8 million
All the code that you write, can be saved into a file with extension .py
You can call python3 and pass in the path to that file to call it.
>>> python3 /code/intro.py
Taking Input:
Python can simply take input during run time using INPUT command. This means you can write a quick code to take user input after the python file is executed. That way you don't need to hardcode the data inside the file.
when you run this, it will prompt you to enter the value:
>>> python3 /code/intro.py
>>> What car do you like? Lamborghini Roadster
>>> You choose: Lamborghini Roadster
Let's dive deeper.
How can we check what was entered by user?
String compare
print('abc' == 'abc')
# True
print('abc' == 'xyz')
# False
This operation is case-sensitive
print('abc' == 'ABC')
# False
print('abc' != 'xyz')
# True
print('abc' != 'abc')
# False
==
is used to check if 2 values are EQUAL
!=
is used to check if 2 values are EQUAL
Partial match: in
, not in
in
operator, which determines if one string contains another string
print('bbb' in 'aaa-bbb-ccc')
# True
print('xxx' in 'aaa-bbb-ccc')
# False
print('abc' in 'aaa-bbb-ccc')
# False
print('xxx' not in 'aaa-bbb-ccc')
# True
print('bbb' not in 'aaa-bbb-ccc')
# False
Note: if you notice above in gray, comments can be added using # symbol
OK! I get it that we can match what's coming in from user, but how do I tell the code to do something based on what was entered?
Let's talk Conditions:
>>> What car do you like? Lamborghini Roadster
>>> You need $507,353 to buy Lamborghini Roadster
IF tells the code to do something in a selective way. If this happens then that happens.
Notice the PRINT line starts a little bit to the right. That's INDENTATION.
By introducing indentation, you tell python that print statement is within IF. So, if the user enters this particular name of the car, only then the print will run.
What happens if user enters a different car?
ELSE:
>>> What car do you like? Honda City
>>> You are aiming too low! Think big
🤣 Think Big.
Notice the first PRINT statement is part of IF. The second PRINT statement is part of ELSE.
This is exactly how the code is written in Python. Maintaining indentation is crucial.
OK! So, that 2 conditions. One If and second Else.
Can I add multiple conditions?
ELIF:
>>> What car do you like? Pagani Huayra
>>> You need $3,500,000 to buy Pagani Huayra
You can add multiple conditions using ELIF.
That's quite a few new things for the first lesson. Come back for Part 2.
Remember - Practice makes Progress.
Comments
Post a Comment