Home » Home » Python Cheat sheet for Beginners

Here is a beginners cheat sheet for Python programming language:

Read Also- Machine Learning with Python

Python Basics

Data Types

read Also-Python Data Types

Python has several data types including:

  • Integers: Whole numbers, such as 1, 2, 3
  • Floats: Decimal numbers, such as 1.0, 1.5, 3.14
  • Booleans: True or False values
  • Strings: A sequence of characters, such as “hello world”
  • Lists: A collection of ordered elements, such as [1, 2, 3]
  • Dictionaries: A collection of key-value pairs, such as {“name”: “John”, “age”: 30}
  • Tuples: A collection of ordered, immutable elements, such as (1, 2, 3)

Variables

Read Also- Variables and DataTypes in Python

In Python, variables can be assigned values using the = operator. For example:

codex = 5
y = "hello"

Printing Output

To print output in Python, use the print() function. For example:

print("hello world")

Arithmetic Operators

Read more-Python Operators

Python supports common arithmetic operators, including:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • // floor division
  • % modulus
  • ** exponentiation

Comparison Operators

Python supports common comparison operators, including:

  • == equal to
  • != not equal to
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to

Logical Operators

Python supports common logical operators, including:

  • and logical AND
  • or logical OR
  • not logical NOT

Conditional Statements

Python supports if-else statements for conditional execution of code. For example:

if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")

Loops

Python supports for and while loops for iterating over collections and executing code repeatedly. For example:

for i in range(10):
print(i)

while x < 10:
print(x)
x += 1

Functions

Read Also- Python Functions Functions in Python

Defining Functions

Functions can be defined in Python using the def keyword. For example:

def square(x):
return x * x

Calling Functions

Functions can be called in Python by passing arguments to them. For example:

result = square(5)
print(result) # prints 25

File I/O

Opening Files

Files can be opened in Python using the open() function. For example:

file = open("example.txt", "r")

Reading Files

Files can be read in Python using the read() method. For example:

content = file.read()
print(content)

Writing to Files

Files can be written to in Python using the write() method. For example:

file = open("example.txt", "w")
file.write("Hello world")
file.close()

Libraries

Read Also-Machine Learning with Python

Importing Libraries

Libraries can be imported in Python using the import keyword. For example:

import math

Using Libraries

Libraries can be used in Python by calling their functions. For example:

result = math.sqrt(16)
print(result) # prints 4.0

Lists

Creating Lists

Lists can be created in Python using square brackets []. For example:my_list = [1, 2, 3]

Accessing List Elements

List elements can be accessed using indexing, starting from 0. For example

print(my_list[0]) # prints 1

Modifying List Elements

List elements can be modified by assigning new values to them. For example:

my_list[0] = 4 
print(my_list) # prints [4, 2, 3]

List Methods

Python provides many useful methods for working with lists, such as:

  • append(): adds an element to the end of the list
  • extend(): adds elements from another list to the end of the list
  • insert(): adds an element at a specific position in the list
  • remove(): removes the first occurrence of an element from the list
  • pop(): removes and returns the element at a specific position in the list
  • index(): returns the index of the first occurrence of an element in the list
  • count(): returns the number of times an element appears in the list
  • sort(): sorts the elements in the list
  • reverse(): reverses the order of the elements in the list

Dictionaries

Creating Dictionaries

Dictionaries can be created in Python using curly braces {}. For example:

my_dict = {"name": "John", "age": 30}

Accessing Dictionary Elements

Dictionary elements can be accessed using keys. For example

print(my_dict["name"]) # prints "John"

Modifying Dictionary Elements

Dictionary elements can be modified by assigning new values to them. For example:

my_dict["age"] = 31 
print(my_dict) # prints {"name": "John", "age": 31}

Dictionary Methods

Python provides many useful methods for working with dictionaries, such as:

  • keys(): returns a list of all the keys in the dictionary
  • values(): returns a list of all the values in the dictionary
  • items(): returns a list of all the key-value pairs in the dictionary
  • get(): returns the value associated with a key, or a default value if the key is not found
  • pop(): removes and returns the value associated with a key
  • update(): updates the dictionary with key-value pairs from another dictionary

Classes

Creating Classes

Classes can be created in Python using the class keyword. For example:

class Person: def __init__(self, name, age): 
self.name = name self.age = age def greet(self): print("Hello, my name is", self.name)

Creating Objects

Objects can be created from classes using the class name and parentheses. For example:

person = Person("John", 30)

Accessing Object Attributes

Object attributes can be accessed using dot notation. For example:

print(person.name) # prints "John"

Calling Object Methods

Object methods can be called using dot notation and parentheses. For example

person.greet() # prints "Hello, my name is John"

That’s a quick overview of some of the basics of Python programming. There’s a lot more to learn, but this should give you a good starting point!

Control Flow

if-else Statements

The if-else statement is used to conditionally execute code based on a boolean expression. For example

if x > 0: 
print("x is positive")
 else: 
print("x is not positive")

for Loops

The for loop is used to iterate over a sequence of elements. For example

for i in range(5): 
print(i)

while Loops

The while loop is used to repeatedly execute code while a boolean expression is true. For example:

i = 0
 while i < 5: 
print(i)
 i += 1

break and continue Statements

The break statement is used to exit a loop prematurely, while the continue statement is used to skip over an iteration of a loop. For example:

for i in range(10):
 if i == 5:
 break
 if i % 2 == 0: 
continue 
print(i)

Functions in Python for Beginners

Beginners can understood the basics of functions by the following topics:

Defining Functions

Functions are defined in Python using the def keyword. For example:

def square(x):
 return x * x

Calling Functions

Functions are called by their name followed by parentheses, with any arguments passed inside the parentheses. For example

print(square(3)) # prints 9

Default Arguments

Functions can have default argument values, which are used if an argument is not provided. For example:

def greet(name="World"): 
print("Hello,", name)
 greet() # prints "Hello, World" greet("John") # prints "Hello, John"

Variable Arguments

Functions can accept a variable number of arguments using the *args syntax. For example:

def add(*args): 
result = 0
 for arg in args:
 result += arg
 return result
 print(add(1, 2, 3)) # prints 6

Modules

Importing Modules

Modules can be imported in Python using the import keyword. For example

import math 
print(math.sqrt(4)) # prints 2.0

Alias Modules

Modules can be aliased using the as keyword. For example

import math as m 
print(m.sqrt(4)) # prints 2.0

Importing Functions from Modules

Individual functions can be imported from modules using the from ... import ... syntax. For example:

from math import sqrt 
print(sqrt(4)) # prints 2.0

Data Structures

Lists

Lists are a sequence of elements that can be accessed by their index. For example:

fruits = ["apple", "banana", "cherry"] 
print(fruits[1]) # prints "banana"

Tuples

Tuples are like lists, but they are immutable, meaning that their values cannot be changed. For example:

point = (3, 4) 
print(point[0]) # prints 3

Dictionaries

Dictionaries are collections of key-value pairs, which can be accessed by their keys. For example:

person = {"name": "John", "age": 30} 
print(person["name"]) # prints "John"

Sets

Sets are collections of unique elements. For example:

fruits = {"apple", "banana", "cherry"} 
fruits.add("apple") 
print(fruits) # prints {"apple", "banana", "cherry"}

Object-Oriented Programming for beginners

OOP in Python for beginners are simply understood by following topics:

Read Also-Python’s Object-Oriented Programming (OOP) Features: A Comprehensive Guide

Classes

Classes define objects with shared properties and methods. For example:

class Person:
 def __init__(self, name, age):
 self.name = name
 self.age = age
def greet(self):
 print("Hello, my name is", self.name) person = Person("John", 30)
 person.greet() # prints "Hello, my name is John"

Inheritance

Inheritance allows classes to inherit properties and methods from parent classes. For example

class Student(Person):
 def __init__(self, name, age, major): super().__init__(name, age) self.major = major def greet(self): super().greet()
 print("I am studying", self.major) 
student = Student("Jane", 20, "Computer Science") 
student.greet() # prints "Hello, my name is Jane" followed by "I am studying Computer Science"

File Input/Output

Opening Files

Files can be opened using the open function. For example:

file = open("example.txt", "r")

Reading Files

Files can be read using the read function. For example

contents = file.read()

Writing to Files

Files can be written to using the write function. For example

file.write("Hello, World!")

Closing Files

Files should be closed using the close function. For example:

file.close()

That’s a quick overview of some of the more advanced features of Python for beginners. With these tools in your toolkit, you’ll be able to write powerful and flexible programs in Python!

Visit for more-https://wiki.python.org/moin/BeginnersGuide

Related Posts

Leave a Reply

%d bloggers like this: