Important Topics For Basic Python Programming

Python is a very vast programming language. Python is one of the most widely used coding languages in the world. It supports a large amount of packages and modules. Python is used in web development, machine learning, web scraping and web automation and lots more in other fields. Python is one of the easiest coding languages in the world. Let’s discuss the basic Python programming language in detail.

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:

  • web development (server-side),
  • software development,
  • mathematics,
  • system scripting
  • Web scraping
  • Web Automation.

Why Python?

  • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
  • Python has a simple syntax similar to the English language.
  • Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
  • Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
  • Python can be treated in a procedural way, an object-oriented way or a functional way.

Let’s Start with “Hello World!”

print(“Hello World!”)

In Python, it is only one line code for printing the hello world as output. In the above example, a print function is present which is used to print the output to the user.

Python Indentation

Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.

if 6 > 1:
	print("Hello PrepBytes!!!")
	#Output: Hello PrepBytes!!!

Python Variables

In Python, variables are created when you assign a value to it. Python will make your workload less as there is no need to initialize any variables.

x = "Hello PrepBytes!!!"
print(x)
#Output: Hello PrepBytes!!!

Built-in Data Types

In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: None

We will see some data types used in some examples in the below topics.

Python Casting

Specify a Variable Type
There may be times when you want to specify a type on a variable. This can be done with casting. Python is an object-oriented language, and as such it uses classes to define data types, including its primitive types.
Casting in python is therefore done using constructor functions.

x = int(1)       # x will be 1
x = float(1)     # x will be 1.0
x = str("1")     # x will be 's1'

Python String

Strings in python are surrounded by either single quotation marks, or double quotation marks.
‘hello’ is the same as "hello".
We can display a string literal with the print() function.

a = "Hello"
print(a)

Python Booleans

In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer.

print(10 > 9)
print(10 == 9)
print(10 < 9)

Mostly values are True

Almost any value is evaluated to True if it has some sort of content.
Any string is True, except empty strings.
Any number is True, except 0.
Any list, tuple, set, and dictionary are True, except empty ones.
The bool() function allows you to evaluate any value, and give you True or False in return.

bool("abc") #True
bool(123) #True
bool(["apple", "cherry", "banana"]) #True
bool([]) #False

Python List

Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set and Dictionary all with different qualities and usage.
Lists are created using square brackets

thislist = ["grapes", "banana", "mangoes"]
print(thislist)

Python Tuple

Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set and Dictionary all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.

mytuple = ("ram","sham", "sita")
print(mytuple)

Python Set

Sets are used to store multiple items in a single variable. A set is a collection which is unordered, unchangeable*, and unindexed.

myset = {"ram","sham", "sita"}
print(myset)

Python Dictionaries

Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

thisdict = {
  "Brand": "Maruti",
  "model": "Brezza",
  "year": 2022
}
print(thisdict)

If, Elif, and Else

Decision making is required when we want to execute a code only if a certain condition is satisfied.
The if…elif…else statement is used in Python for decision making.

num = 3

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

While Loop

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
  print(i)
  i += 1

For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc

for x in "prepbytes":
	print(x)

Functions

A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
In Python a function is defined using the def keyword.

def my_function():
  print("Hello from PrepBytes")

my_function()

FAQ Related to Basic Python Programming

  1. What is syntax in Python?
    The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers).

  2. What are the basics in Python programming?
    Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.

  3. What is a loop in Python?
    The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.

This article tried to discuss basic python programming topics. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

Your email address will not be published. Required fields are marked *