Docsity

Learn Python In 1 Hours Fixed Notes, Cheat Sheet of Computer science

Toggle sidebar
bg1
Learn Python in 1 Hour - Quick Notes
1. Basics
Print something:
print("Hello, World!")
Variables:
x = 5
name = "Alice"
Data Types:
- int (5), float (5.5), str ("hello"), bool (True/False), list, dict, tuple, set
Input from user:
name = input("Enter your name: ")
2. Conditions
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
3. Loops
For loop:
for i in range(5):
print(i)
While loop:
while x > 0:
pf3
pf4

Partial preview of the text

Download Learn Python In 1 Hours Fixed Notes and more Cheat Sheet Computer science in PDF only on Docsity!

1. Basics

Print something: print("Hello, World!")

Variables: x = 5 name = "Alice"

Data Types:

  • int (5), float (5.5), str ("hello"), bool (True/False), list, dict, tuple, set

Input from user: name = input("Enter your name: ")

2. Conditions

if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")

3. Loops

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

While loop: while x > 0:

print(x) x -= 1

4. Functions

def greet(name): return f"Hello, {name}!"

print(greet("Alice"))

5. Lists

fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits[0]) # first item

Loop through list: for fruit in fruits: print(fruit)

6. Dictionaries

person = {"name": "Alice", "age": 25} print(person["name"])

Add new key-value: person["city"] = "New York"

7. Importing Modules

import math print(math.sqrt(16))

  • Indentation matters (use 4 spaces).
  • Comments: # this is a comment
  • Pip: pip install package-name
  • Popular libraries: numpy, pandas, flask, django