Python Basics

MercifulFreesia avatar
MercifulFreesia
·
·
Download

Start Quiz

Study Flashcards

5 Questions

What is used to define block-level structure in Python?

Indentation

How do you assign a value to a variable in Python?

Using the assignment operator (=)

What type of data does the 'bool' data type represent?

True or false values

Which of the following is NOT an arithmetic operator in Python?

==

What is the purpose of single quotes or double quotes in Python?

To enclose a string

Study Notes

Syntax

  • Indentation: Python uses indentation (spaces or tabs) to define block-level structure.
  • Variables: Assign a value to a variable using the assignment operator (=).
  • Basic Data Types:
    • Integers (int): whole numbers
    • Floats (float): decimal numbers
    • Strings (str): sequences of characters, can be enclosed in single quotes or double quotes
    • Boolean (bool): true or false values
  • Operators:
    • Arithmetic operators: +, -, *, /, %
    • Comparison operators: ==, !=, >, <, >=, <=
    • Logical operators: and, or, not
  • Control Flow:
    • Conditional statements: if, elif, else
    • Loops: for, while
    • Functions: def

Modules and Packages

  • Modules: A file containing Python definitions and statements.
  • Packages: A directory containing multiple modules and a special file __init__.py.
  • Importing Modules:
    • Import a module: import module_name
    • Import a specific function or variable: from module_name import function_or_variable
    • Import all functions and variables: from module_name import * (not recommended)
  • Creating a Module:
    • Create a new Python file with a .py extension.
    • Define functions, variables, and classes in the file.
    • Import the module in another Python file using import statement.

Object-Oriented Programming

  • Classes: Define a class using the class keyword.
  • Objects: Instances of a class, created using the () operator.
  • Attributes: Data associated with an object, accessed using dot notation (e.g., object.attribute).
  • Methods: Functions associated with a class, accessed using dot notation (e.g., object.method()).
  • Inheritance: A child class can inherit attributes and methods from a parent class using the class Child(Parent): syntax.

File Input/Output

  • Reading Files:
    • Open a file in read mode: open('file.txt', 'r')
    • Read the contents: file.read() or file.readline() or file.readlines()
    • Close the file: file.close() (or use with statement)
  • Writing Files:
    • Open a file in write mode: open('file.txt', 'w')
    • Write to the file: file.write('content')
    • Close the file: file.close() (or use with statement)
  • Modes:
    • 'r': read mode
    • 'w': write mode
    • 'a': append mode
    • 'r+', 'w+', 'a+': read and write modes
  • JSON and CSV:
    • Use the json module to work with JSON data.
    • Use the csv module to work with CSV data.

Syntax in Python

  • Python uses indentation to define block-level structure, which can be done using spaces or tabs.

Variables in Python

  • Assign a value to a variable using the assignment operator (=).

Basic Data Types in Python

  • Integers (int) are whole numbers.
  • Floats (float) are decimal numbers.
  • Strings (str) are sequences of characters and can be enclosed in single quotes or double quotes.
  • Boolean (bool) values are either true or false.

Operators in Python

  • Arithmetic operators include +, -, *, /, and %.
  • Comparison operators include ==, !=, >, and <=.

Test your knowledge of Python fundamentals, including syntax, data types, and operators.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Python Syntax Errors Quiz
10 questions
Python Syntax and Design Philosophy Quiz
6 questions
Python Syntax and Basics
5 questions
Quiz de programmation Python
10 questions

Quiz de programmation Python

ComprehensiveDiscernment avatar
ComprehensiveDiscernment
Use Quizgecko on...
Browser
Browser