Learn Python From Scratch 10 Essential Basics Every Beginner

Kenji Sato
-
learn python from scratch 10 essential basics every beginner

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. First Python Program Once installed, we can write and execute Python code using an Integrated Development Environment (IDE) like PyCharm, Vs Code (requires installing Python extension) etc.

print("Hello Geeks, Welcome to Python Basics") Output 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 Python 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. # This is a single-line comment """ This is a multi-line comment or docstring.

""" Explanation: - #: Denotes a single-line comment. - """ """ or ''' ''': Triple quotes are used for multi-line comments or docstrings. Variables in Python 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: - Must start with a letter (a–z, A–Z) or an underscore (_) - Cannot start with a number - Can contain only letters, numbers, and underscores - Variable names are case-sensitive (name, Name, and NAME are different) - Python keywords cannot be used as variable names # Integer assignment a = 45 # Floating-point assignment b = 1456.8 # String assignment c = "Geek" print(a) print(b) print(c) Output 45 1456.8 Geek Data Types in Python 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.

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 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. val = input("Enter your value: ") print("You entered:", val) 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.

name = input("Enter your name: ") age = int(input("Enter your age: ")) print(type(name)) print(type(age)) Output Enter your name: Jake Enter your age: 12 <class 'str'> <class 'int'> Explanation: - input() always returns a string. - int() converts the input into an integer. - Use float() to convert input into a decimal value. 2. Output: The print() function is used to display values or messages. print "Hello, Geek!" Output Hello, Geek!

Python Operators 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: - P - Parentheses - E - Exponentiation - M - Multiplication (Multiplication and division have the same precedence) - D - Division - A - Addition (Addition and subtraction have the same precedence) - S - Subtraction a = 9 b = 4 add = a + b sub = a - b mul = a * b mod = a % b exp = a ** b print(add) print(sub) print(mul) print(mod) print(exp) Output 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.

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 Output False True False True False True Explanation: - Each print() checks a condition.

The result is either True or False depending on whether the comparison holds. - These operators are commonly used to control program flow in if statements, loops, etc. 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. a = True b = False print(a and b) print(a or b) print(not a) Output 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: &, |, ^, ~, <<, >> a = 10 b = 4 print(a & b) print(a | b) print(~a) print(a ^ b) print(a >> 2) print(a << 2) Output 0 14 -11 14 2 40 5. Assignment Operators: These are used to assign values to the variables. Types of assignment operators: =, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, >>=, <<=.

a = 10 b = a print(b) b += a print(b) b -= a print(b) b *= a print(b) b <<= a print(b) Output 10 20 10 100 102400 Python If Else 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 = 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") Output i is greater than 15 i'm in else Block i'm not in if and not in else Block Explanation: - The condition i < 15 is False, so the else block is executed.

The last print() runs no matter what because it's outside the if-else structure. 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 = 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") Output i is 20 Explanation: - Python checks conditions from top to bottom.

Once a condition is True, it executes that block and skips the rest. - If none of the conditions match, it executes the else block. Python Loops 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.

for i in range(0, 10, 2): print(i) Output 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.

count = 0 while (count < 3): count = count + 1 print("Hello Geek") Output Hello Geek Hello Geek Hello Geek Explanation: - The loop runs 3 times because count goes from 0 - 1 - 2. - Once count becomes 3, the condition count < 3 becomes False and the loop stops. 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: - Built-in functions like: print(), len(), type() - User-defined functions created using the def keyword Example: This example shows a simple user-defined function that checks whether a number is even or odd using a conditional statement. def evenOdd(x): if x % 2 == 0: print("even") else: print("odd") evenOdd(2) evenOdd(3) Output even odd Explanation: - The function evenOdd(x) checks if x is divisible by 2. - If it is, it prints "even"; otherwise, "odd".

What's Next After learning Python basics, you can move forward in these areas: - Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels. - Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators. - Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), TensorFlow/PyTorch (ML), and Selenium/BeautifulSoup (automation). - Build Python Projects: Apply your knowledge by creating real-world Python projects.

People Also Asked

Python Basics: 10 Fundamentals Every Beginner Should Master?

What's Next After learning Python basics, you can move forward in these areas: - Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels. - Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators. - Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), Tenso...

Learn Python from Scratch: 10 Essential Basics Every Beginner Must ...?

What's Next After learning Python basics, you can move forward in these areas: - Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels. - Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators. - Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), Tenso...

Python Basics: The Complete Step-by-Step Guide for Beginners?

Once a condition is True, it executes that block and skips the rest. - If none of the conditions match, it executes the else block. Python Loops 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...

Learn Python Basics - GeeksforGeeks?

What's Next After learning Python basics, you can move forward in these areas: - Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels. - Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators. - Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), Tenso...

Python Basics - Real Python?

What's Next After learning Python basics, you can move forward in these areas: - Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels. - Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators. - Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), Tenso...