Beginning Python for Non-Programmers
  • Cover
  • About the Author
  • Introduction to Programming
    • What is Programming?
    • What Programmers Do
    • The Mind of a Programmer
    • How Does a Programmer Think?
  • Programming Languages
    • Python
    • HTML / CSS
    • JavaScript / TypeScript
    • C, C++, C#
    • Java
    • SQL
  • Introduction to Python
    • What is Python?
    • Types of Python Programs
    • Core Tools for Python Programming
      • Python Interpreter
      • Jupyter Notebooks
      • Visual Studio Code
      • Python Playground
      • Pycharm Community Edition
  • Installing Python
    • Step-by-step guide for Windows Users
    • Step-by-step guide for Mac Users
  • Installing Visual Studio Code
    • Step-by-step guide for Windows Users
    • Step-by-step guide for Mac Users
  • Writing and Running Your First Python Program
  • Basic Concepts of Python Programming
  • Control Flow in Python
  • Functions and Reusability
  • Debugging and Problem Solving
  • Exploring More Python Concepts
  • Adding Python Packages with pip
  • Python Web Frameworks
  • Resources for Continued Learning
    • CodeAcademy.com
    • Python.org
    • SoloLearn.com
    • coursera.com
    • udemy.com
    • youtube.com
    • Code With Mosh
    • GameDev.tv
  • Staying Motivated and Practicing Regularly
  • Appendices
    • Beginner Project Ideas for Learning
    • Python Cheat Sheet
    • Frequently Asked Questions for Beginners
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Appendices

Python Cheat Sheet

This cheat sheet covers some of the most commonly used Python syntax and functions, providing a quick reference for key concepts as you write and debug your programs.

Variables and Data Types

  • Integer: A whole number, e.g., x = 10

  • Float: A decimal number, e.g., y = 3.14

  • String: A sequence of characters, e.g., name = "Alice"

  • Boolean: A true or false value, e.g., is_valid = True

Basic Operations

  • Addition: x + y

  • Subtraction: x - y

  • Multiplication: x * y

  • Division: x / y

  • Exponentiation: x ** y (x raised to the power of y)

  • Modulo: x % y (remainder of x divided by y)

String Operations

  • Concatenation: "Hello" + "World" → "HelloWorld"

  • Length of a String: len("Hello") → 5

  • String Methods:

    • lower(): "Hello".lower() → "hello"

    • upper(): "Hello".upper() → "HELLO"

    • replace(): "Hello".replace("H", "J") → "Jello"

Lists

  • Creating a List: my_list = [1, 2, 3]

  • Accessing Items: my_list[0] → 1

  • Appending an Item: my_list.append(4) → [1, 2, 3, 4]

  • Removing an Item: my_list.remove(2) → [1, 3, 4]

  • List Length: len(my_list) → 3

Dictionaries

  • Creating a Dictionary: my_dict = {"name": "Alice", "age": 25}

  • Accessing Values by Key: my_dict["name"] → "Alice"

  • Adding a Key-Value Pair: my_dict["email"] = "alice@example.com"

  • Removing a Key-Value Pair: my_dict.pop("age") → {"name": "Alice", "email": "alice@example.com"}

Control Flow

  • If Statement: if x > 10: print("x is greater than 10") elif x == 10: print("x is equal to 10") else: print("x is less than 10")

  • For Loop: for item in my_list: print(item)

  • While Loop: while x < 5: print(x) x += 1

Functions

  • Defining a Function: def greet(name): return "Hello, " + name

  • Calling a Function: print(greet("Alice")) # Outputs: Hello, Alice

Input and Output

  • Printing to the Console: print("Hello, World!")

  • Getting User Input: name = input("What is your name? ")

Exception Handling

  • Try-Except Block: try: result = 10 / 0 except ZeroDivisionError: print("You can't divide by zero!")

Importing Modules

  • Importing a Module: import math print(math.sqrt(16)) # Outputs: 4.0

  • Installing and Using External Libraries (with pip): pip install requests

File Handling

  • Reading from a File: with open("file.txt", "r") as file: contents = file.read()

  • Writing to a File: with open("file.txt", "w") as file: file.write("Hello, World!")

Common Built-in Functions

  • Range: range(5) → Generates numbers from 0 to 4.

  • Type: type(x) → Returns the data type of x.

  • len(): len(my_list) → Returns the number of items in a list, string, etc.

  • max() / min(): max(my_list) → Returns the largest element; min(my_list) → Returns the smallest.

List Comprehension

  • Basic List Comprehension: squares = [x**2 for x in range(5)] # Outputs: [0, 1, 4, 9, 16]

Conclusion

This cheat sheet provides a quick reference to some of the most common Python concepts and syntax. As you continue learning, you’ll discover many more powerful features that Python offers. Keep this handy as you write code and explore new Python concepts!

Some content generated with AI

PreviousBeginner Project Ideas for LearningNextFrequently Asked Questions for Beginners

Last updated 9 months ago

Was this helpful?