Python is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial intelligence, and more. In this article, we'll cover the foundational concepts of Python programming to help you get started.
Writing First Python ProgramTo begin coding in Python, we'll need to have Python installed on our system. You can download the latest version from the official Python website. Once installed, we can write and execute Python code using an Integrated Development Environment (IDE) like PyCharm, Vs Code (requires installing Python extension), or even a simple text editor.
Python
print("Hello Geeks, Welcome to Python Basics")
Hello Geeks, Welcome to Python Basics
Explanation: print() is a built-in function that outputs text or variables to the console. In this case, it displays the string "Hello, Geeks! Welcome to Python Basics".
Comments in PythonComments in Python are the lines in the code that are ignored by the interpreter during the execution of the program. Also, Comments enhance the readability of the code and help the programmers to understand the code very carefully.
Python
# This is a single-line comment
"""
This is a
multi-line comment
or docstring.
"""
Explanation:
Python Variable is a container that store values. Python is not “statically typed”. An Example of a Variable in Python is a representational name that serves as a pointer to an object. Once an object is assigned to a variable, it can be referred to by that name.
Rules for Naming VariablesExample:
Python
# Integer assignment
age = 45
# Floating-point assignment
salary = 1456.8
# String assignment
name = "Geek"
print(age)
print(salary)
print(name)
Data Types in Python
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.
Example: This code assigns variable ‘x’ different values of various data types in Python.
Python
x = "Hello World" # string
x = 50 # integer
x = 60.5 # float
x = 3j # complex
x = ["geeks", "for", "geeks"] # list
x = ("geeks", "for", "geeks") # tuple
x = {"name": "Suraj", "age": 24} # dict
x = {"geeks", "for", "geeks"} # set
x = True # bool
x = b"Geeks" # binary
Python Input/Output
Python provides simple functions for input and output operations.
InputThe input() function allows user input, example:
Python
val = input("Enter your value: ")
print("You entered:", val)
Output:
Enter your value: 11
You entered: 11
The input() function in Python always returns data as a string, regardless of what the user enters. If we want to store the input as another data type (like int, float, etc.), we need to explicitly convert (typecast) it.
Example:
Python
name = input("Enter your name: ")
print(type(name))
age = int(input("Enter your age: "))
print(type(age))
Output:
Enter your name: Geeks
<class 'str'>
Enter your age: 8
<class 'int'>
Explanation:
# Python program show input and Output
val = input("Enter your value: ")
print(val)
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Arithmetic OperatorsPython Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. Types of arithmetic operators: +, -, *, /, //, %, **
Precedence of Arithmetic OperatorsThe precedence of Arithmetic Operators in Python is as follows:
Example:
Python
a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
mod = a % b
E = a ** b
print(add)
print(sub)
print(mul)
print(mod)
print(E)
Comparison Operators
Comparison operators are used to compare two values. They return a Boolean value — either True or False — depending on whether the comparison is correct. These operators are often used in conditional statements like if, while, and loops.
Example:
Python
a = 10
b = 20
print(a == b) # False, because 10 is not equal to 20
print(a != b) # True, because 10 is not equal to 20
print(a > b) # False, 10 is not greater than 20
print(a < b) # True, 10 is less than 20
print(a >= b) # False, 10 is not greater than or equal to 20
print(a <= b) # True, 10 is less than or equal to 20
False True False True False True
Explanation:
Python Logical operators perform Logical AND , Logical OR , and Logical NOT operations. It is used to combine conditional statements. Types of logicsl operators: and, or, not.
Python
a = True
b = False
print(a and b)
print(a or b)
print(not a)
Bitwise Operators
Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers. Types of bitwise operators: &, |, ^, ~, <<, >>
Python
a = 10
b = 4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
Assignment Operators
Python Assignment operators are used to assign values to the variables. Types of assignment operators: =, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, >>=, <<=.
Python
a = 10
b = a
print(b)
b += a
print(b)
b -= a
print(b)
b *= a
print(b)
b <<= a
print(b)
10 20 10 100 102400Python If Else
In Python, the if statement is used to run a block of code only when a specific condition is true. If the condition is false and you want to run a different block of code, you can use the else statement. This allows your program to make decisions and respond differently based on conditions.
Example 1: Python IF-Else Python
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
i is greater than 15 i'm in else Block i'm not in if and not in else Block
Explanation:
Sometimes, weneed to check multiple conditions. In such cases, Python provides the if-elif-else structure.
Python
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Explanation:
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple, List, Set, or Dictionary. Here, we will see a "for" loop in conjunction with the range() function to generate a sequence of numbers starting from 0, up to (but not including) 10, and with a step size of 2. For each number in the sequence, the loop prints its value using the print() function.
Python
for i in range(0, 10, 2):
print(i)
Explanation:
A while loop continues to execute as long as a condition is True. In this example, the condition for while will be True as long as the counter variable (count) is less than 3.
Python
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
Hello Geek Hello Geek Hello Geek
Explanation:
Python Functions
Python Function is a block of reusable code that performs a specific task. Functions help make your code modular, readable, and easier to debug.
There are two main types of functions in Python:
def evenOdd(x):
if x % 2 == 0:
print("even")
else:
print("odd")
evenOdd(2)
evenOdd(3)
Explanation:
After understanding the Python Basics, there are several paths you can explore to further enhance your skills and delve deeper into the language:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4