Python Tutorial: Getting Started with the Basics

 

Python Tutorial: Getting Started with the Basics

1. Installing Python

  • Download from python.org and install.

  • Verify installation by running python --version in your terminal.

2. Writing Your First Python Program

Create a file hello.py with:

python
print("Hello, world!")

Run it with:

bash
python hello.py

3. Variables and Data Types

python
name = "Alice" # String age = 25 # Integer height = 5.6 # Float is_student = True # Boolean

4. Control Flow

If-else:

python
if age >= 18: print("Adult") else: print("Minor")

Loops:

python
for i in range(5): print(i) count = 0 while count < 5: print(count) count += 1

5. Functions

python
def greet(name): return f"Hello, {name}!" print(greet("Alice"))

6. Lists and Dictionaries

python
fruits = ["apple", "banana", "cherry"] print(fruits[1]) # banana person = {"name": "Alice", "age": 25} print(person["name"]) # Alice

7. Importing Modules

python
import math print(math.sqrt(16)) # 4.0

Python Tutorial: Getting Started with the Basics

  1. Installing Python
    Download Python from python.org and install it. After installation, open your terminal or command prompt and type python --version to check if it’s installed correctly.

  2. Writing Your First Python Program
    Create a file named hello.py and write the following line:
    print("Hello, world!")
    Run it by typing python hello.py in your terminal.

  3. Variables and Data Types
    Python supports various data types:

  • Strings: name = "Alice"

  • Integers: age = 25

  • Floats: height = 5.6

  • Booleans: is_student = True

  1. Control Flow
    Use if-else statements for decision making:
    If age is 18 or more, print "Adult", else print "Minor".
    Loops allow repeated execution:

  • For loop example: Loop from 0 to 4 and print each number.

  • While loop example: Print numbers while a condition is true.

  1. Functions
    Define reusable blocks of code with functions. Example: A function greet takes a name and returns a greeting string.

  2. Lists and Dictionaries

  • Lists are ordered collections: fruits = ["apple", "banana", "cherry"]

  • Dictionaries store key-value pairs: person = {"name": "Alice", "age": 25}

  1. Importing Modules
    Python has many built-in modules. For example, import the math module to use mathematical functions like square root.

Comments

Popular posts from this blog

C++: The Language Behind High-Performance Systems and Real-Time Applications

Rust vs Go: Modern Languages for System and Cloud Applications

The Evolution of Programming Languages in the Era of Cutting-Edge Technologies