Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for A friendly beginners Python 3 Guide: Where to start
Digital Pollution profile imageLeandro Nuñez
Leandro Nuñez forDigital Pollution

Posted on • Edited on

     

A friendly beginners Python 3 Guide: Where to start

TL;DR:

Welcome toA friendly beginners Python 3 Guide: Where to start. I'm thrilled to be your guide on this learning journey. Fromsetting up your Python environment to diving deep intodata types,control flow,functions,OOP, and more, this guide has got you covered.

Let's begin!

Introduction:

Hello there!

I'm Leandro, a passionate software engineer, and I'm thrilled to embark on this Python 3 adventure with you.

Python is an elegant and versatile language that has won the hearts of developers worldwide.

Whether you're a newcomer or an experienced programmer, this guide will equip you with all the essential knowledge you need to excel in Python 3.

So, let's dive in and start coding!

Table of Contents:

  1. Setting up the Environment for Python 3
  2. Python 3 Basics: Data Types, Variables, and Operators
  3. Control Flow: Conditional Statements and Loops
  4. Functions and Modules
  5. Error Handling
  6. Object-Oriented Programming (OOP)
  7. Exception Handling
  8. Modules and Packages
  9. File Handling
  10. Regular Expressions

Content:

1. Setting up the Environment for Python 3:

To start our Python journey, let's install Python 3 and verify the version:

python3--version
Enter fullscreen modeExit fullscreen mode

Output:

Python 3.x.x
Enter fullscreen modeExit fullscreen mode

We'll also set up a virtual environment for managing project dependencies:

python3-mvenvmyenvsourcemyenv/bin/activate# On Windows, use: myenv\Scripts\activate
Enter fullscreen modeExit fullscreen mode

Output:
(Virtual environment activated)

2. Python 3 Basics: Data Types, Variables, and Operators:


i. Data Types:
Python supports various data types.
Let's explore some of them:

age=25name="Leandro"height=1.75is_student=Truefavorite_fruits=["apple","banana","orange"]
Enter fullscreen modeExit fullscreen mode


ii. Variables:
Variables store values, and their data type can change dynamically:

x=10x="hello"
Enter fullscreen modeExit fullscreen mode


iii. Operators:
Python offers a variety of operators for different operations:

# Arithmetic Operatorsx=10+5y=8-3z=3*4w=15/3# Comparison Operatorsis_equal=x==yis_greater=x>yis_not_equal=x!=y# Logical Operatorsis_both_true=is_studentandx>yis_either_true=is_studentorx>yis_not_true=notis_student
Enter fullscreen modeExit fullscreen mode

Output:

x = 15y = 5z = 12w = 5.0is_equal = Falseis_greater = Trueis_not_equal = Trueis_both_true = Trueis_either_true = Trueis_not_true = False
Enter fullscreen modeExit fullscreen mode

3. Control Flow: Conditional Statements and Loops:


i. Conditional Statements:
Control the flow of your program usingif,elif, andelse:

ifx>y:print("x is greater than y")elifx==y:print("x and y are equal")else:print("x is smaller than y")
Enter fullscreen modeExit fullscreen mode

Output:

x is greater than y
Enter fullscreen modeExit fullscreen mode


ii. Loops:
Usefor andwhile loops for repetitive tasks:

forfruitinfavorite_fruits:print(f"Enjoy eating{fruit}")count=0whilecount<5:print(f"Count is{count}")count+=1
Enter fullscreen modeExit fullscreen mode

Output:

Enjoy eating appleEnjoy eating bananaEnjoy eating orangeCount is 0Count is 1Count is 2Count is 3Count is 4
Enter fullscreen modeExit fullscreen mode

4. Functions and Modules:

Create reusable functions with parameters and return values:

defadd_numbers(a,b):returna+bsum_result=add_numbers(10,5)print(f"The sum is:{sum_result}")
Enter fullscreen modeExit fullscreen mode

Output:

The sum is: 15
Enter fullscreen modeExit fullscreen mode

5. Error Handling:

Handle errors gracefully usingtry-except blocks:

try:result=10/0exceptZeroDivisionError:print("Cannot divide by zero!")
Enter fullscreen modeExit fullscreen mode

Output:

Cannot divide by zero!
Enter fullscreen modeExit fullscreen mode

6. Object-Oriented Programming (OOP):

Implement classes and objects for object-oriented programming:

classDog:def__init__(self,name):self.name=namedefbark(self):return"Woof!"my_dog=Dog("Buddy")print(my_dog.name)print(my_dog.bark())
Enter fullscreen modeExit fullscreen mode

Output:

BuddyWoof!
Enter fullscreen modeExit fullscreen mode

7. Exception Handling:

Customize and raise exceptions for specific cases:

classMyCustomException(Exception):passtry:raiseMyCustomException("This is a custom exception")exceptMyCustomExceptionase:print(f"Caught custom exception:{e}")
Enter fullscreen modeExit fullscreen mode

Output:

Caught custom exception: This is a custom exception
Enter fullscreen modeExit fullscreen mode

8. Modules and Packages:

Organize code into modules and packages for better project structure:

# Create a module named my_module.pydefsay_hello():print("Hello from my_module!")# In another file, import and use the moduleimportmy_modulemy_module.say_hello()
Enter fullscreen modeExit fullscreen mode

Output:

Hello from my_module!
Enter fullscreen modeExit fullscreen mode

9. File Handling:

Read and write files using Python:

# Write to a filewithopen("output.txt","w")asfile:file.write("Hello, this is written to a file.")# Read from a filewithopen("output.txt","r")asfile:content=file.read()print(content)
Enter fullscreen modeExit fullscreen mode

Output:

Hello, this is written to a file.
Enter fullscreen modeExit fullscreen mode

10. Regular Expressions:

Use regular expressions for powerful pattern matching:

importrepattern=r"\b\w{3}\b"text="The cat and dog are running."matches=re.findall(pattern,text)print(matches)
Enter fullscreen modeExit fullscreen mode

Output:

['The', 'cat', 'and', 'dog']
Enter fullscreen modeExit fullscreen mode

Hands-on Experience:

Throughout this guide, you've gained hands-on experience with Python 3, mastering its core concepts and features.

Now, you're equipped to build exciting applications and solve real-world problems with confidence.

Conclusion:

Congratulations on completingThe Ultimate Python 3 Guide!

You've acquired a solid foundation in Python 3, empowering you to build exciting applications and solve real-world problems.

Keep honing your skills and exploring Python's vast ecosystem.

Happy coding, and may the force -of Python 3- be with you!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Custom software development.

More fromDigital Pollution

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp