![]() |
VOOZH | about |
Python is a high-level programming language with a simple and readable syntax. It is commonly used for web development, data analysis, automation and machine learning. This article covers the basic concepts of Python to help beginners start coding.
To begin, install Python on your system 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) etc.
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 are lines in a program that are not executed by the interpreter. They are used to explain code and make it easier to read and understand.
Explanation:
Variables are names that store data in memory. They are created when a value is assigned, with the name referencing that value. Python is dynamically typed, so no data type declaration is required. Rules for naming variables in Python are:
45 1456.8 Geek
Data types define the kind of data a variable can hold and determine the operations that can be performed on it. In Python, every value is an object and each object belongs to a specific data type (class).
Example: The following example shows how the same variable can store values of different data types in Python.
Python provides simple built-in functions to take input from the user and display output on the screen.
1. Input: The input() function is used to take input from the user. By default, the value entered by the user is stored as a string.
Output
Enter your value: 11
You entered: 11
If you need the input in another data type (such as int or float), you must convert it explicitly.
Output
Enter your name: Jake
Enter your age: 12
<class 'str'>
<class 'int'>
Explanation:
2. Output: The print() function is used to display values or messages.
Hello, Geek!
Operators in Python are symbols used to perform operations on values and variables, such as calculations, comparisons, and logical checks.
1. Arithmetic Operators: These are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. Types of arithmetic operators: +, -, *, /, //, %, **. Precedence of Arithmetic Operators are as follows:
13 5 36 1 6561
2. Comparison Operators: These are used to compare two values. They return a Boolean value either True or False depending on whether the comparison is correct.
False True False True False True
Explanation:
3. Logical Operators: It perform Logical AND, Logical OR and Logical NOT operations. It is used to combine conditional statements. Types of logical operators are: AND, OR, NOT.
False True False
4. Bitwise Operators: This act on bits and perform bit-by-bit operations. These are used to operate on binary numbers. Types of bitwise operators are: &, |, ^, ~, <<, >>
0 14 -11 14 2 40
5. Assignment Operators: These are used to assign values to the variables. Types of assignment operators: =, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, >>=, <<=.
10 20 10 100 102400
In Python, the if statement runs a block of code when a condition is True. If the condition is False, the else block runs. This helps programs make decisions based on conditions.
Example 1: This example checks whether the value of i is less than 15. If the condition is true, the if block runs; otherwise, the else block runs.
i is greater than 15 i'm in else Block i'm not in if and not in else Block
Explanation:
Example 2: This example checks multiple conditions for the value of i. Python evaluates each condition in order and executes the block where the condition becomes true.
i is 20
Explanation:
1. For Loop: It is used to iterate over a sequence such as a list, string, or a range of numbers. It runs a block of code once for each item in the sequence. Below example uses range() to generate numbers from 0 to 9 with a step of 2 and prints each value.
0 2 4 6 8
Explanation: range(0, 10, 2) generates numbers: 0, 2, 4, 6, 8 and loop prints each number on a new line.
2. While Loop: It continues to execute as long as a condition is True. In below example, the condition for while will be True as long as the counter variable (count) is less than 3.
Hello Geek Hello Geek Hello Geek
Explanation:
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:
Example: This example shows a simple user-defined function that checks whether a number is even or odd using a conditional statement.
even odd
Explanation:
After learning Python basics, you can move forward in these areas: