0% found this document useful (0 votes)
16 views

Introduction To Python Programming

Python coding

Uploaded by

dundigoshika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Introduction To Python Programming

Python coding

Uploaded by

dundigoshika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Quick Reference Guide to Common Grammar Rules

1. Introduction

Python is a high-level, interpreted programming language known for its easy-to-read syntax and

versatility. It's widely used in web development, data analysis, artificial intelligence, scientific

computing, and more.

2. Setting Up Python

To get started with Python, you need to install it on your computer. You can download the latest

version from the official Python website (python.org) and follow the installation instructions for your

operating system.

3. Basic Syntax

Python uses indentation to define code blocks, instead of braces or keywords. Here's an example of

a simple Python program:

Example:

```

print('Hello, World!')

```

4. Variables and Data Types

In Python, you can create variables to store data. Python supports various data types, including

integers, floats, strings, and booleans.


Quick Reference Guide to Common Grammar Rules

Example:

```

x=5

pi = 3.14

name = 'Alice'

is_student = True

```

5. Control Structures

Python provides several control structures, such as if-else statements and loops, to control the flow

of your program.

Example:

```

if x > 0:

print('x is positive')

else:

print('x is non-positive')

for i in range(5):

print(i)

```

6. Functions
Quick Reference Guide to Common Grammar Rules

Functions allow you to organize your code into reusable blocks. You can define a function using the

`def` keyword.

Example:

```

def greet(name):

return f'Hello, {name}!'

print(greet('Alice'))

```

7. Lists and Dictionaries

Lists and dictionaries are common data structures in Python. Lists are ordered collections, while

dictionaries store key-value pairs.

Example:

```

fruits = ['apple', 'banana', 'cherry']

prices = {'apple': 0.99, 'banana': 0.50, 'cherry': 2.99}

print(fruits[1]) # Output: banana

print(prices['cherry']) # Output: 2.99

```

8. File Handling
Quick Reference Guide to Common Grammar Rules

Python makes it easy to work with files. You can read from and write to files using the built-in `open`

function.

Example:

```

with open('example.txt', 'w') as file:

file.write('Hello, World!')

with open('example.txt', 'r') as file:

content = file.read()

print(content)

```

9. Exception Handling

Python provides a way to handle errors and exceptions using try-except blocks.

Example:

```

try:

result = 10 / 0

except ZeroDivisionError:

print('Cannot divide by zero')

```

10. Modules and Packages


Quick Reference Guide to Common Grammar Rules

You can organize your Python code into modules and packages. A module is a single file, while a

package is a directory of modules.

Example:

```

import math

print(math.sqrt(16)) # Output: 4.0

```

You might also like