Site icon crypticknwoledge.com

Python Basics

  1. Introduction to Python: This tutorial covers the basics of Python, including data types, variables, loops, and control structures.

Examples:

# Declare variables of different data types
x = 5 # integer
y = "Hello" # string
z = 3.14 # float
is_active = True # boolean

# Print the data type of a variable
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'str'>
print(type(z)) # Output: <class 'float'>
print(type(is_active)) # Output: <class 'bool'>

# Declare variables and assign values
x = 5
y = 3

# Perform basic math operations with variables
print(x + y) # Output: 8
print(x - y) # Output: 2
print(x * y) # Output: 15
print(x / y) # Output: 1.6666666666666667

  1. Python Data Types: In this tutorial, you will learn about the different data types in Python, including integers, floats, strings, and booleans.

Examples:

# Declare a string
string = "Hello World"

# Get the length of a string
print(len(string)) # Output: 11

# Access a character in a string using its index
print(string[0]) # Output: 'H'

# Concatenate strings using the + operator
greeting = "Hello"
name = "John"
message = greeting + " " + name + "!"
print(message) # Output: "Hello John!"

# Format strings using the format method
age = 30
formatted_string = "I am {} years old.".format(age)
print(formatted_string) # Output: "I am 30 years old."

  1. Python Variables: This tutorial covers how to define and use variables in Python, as well as how to perform basic math operations with them.

Examples:

# Declare variables and assign values
x = 5
y = 3

# Perform basic math operations with variables
print(x + y) # Output: 8
print(x - y) # Output: 2
print(x * y) # Output: 15
print(x / y) # Output: 1.6666666666666667

  1. Python Loops: This tutorial covers the different looping constructs in Python, including for loops and while loops.

Examples:

# Loop through a list and print each element
colors = ["red", "green", "blue"]
for color in colors:
  print(color)

# Output:
# red
# green
# blue

# Loop through a range of numbers and print each one
for i in range(5):
  print(i)

# Output:
# 0
# 1
# 2
# 3
# 4

# Loop while a condition is true
i = 0
while i < 5:
  print(i)
  i += 1

# Output:
# 0
# 1
# 2
# 3
# 4

# Loop until a condition is true
i = 0
while True:
  print(i)
  i += 1
  if i == 5:
    break

# Output:
# 0
# 1
# 2
# 3
# 4

# Skip an iteration with continue
for i in range(5):
  if i == 3:
    continue
  print(i)

# Output:
# 0
# 1
# 2
# 4

# Exit a loop with break
for i in range(5):
  if i == 3:
    break
  print(i)

# Output:
# 0
# 1
# 2

5. Python Functions:

def add(a, b):
  return a + b

result = add(3, 4)
print(result)  # Output: 7

def greet():
  print("Hello, world!")

greet()  # Output: "Hello, world!"

def greet(name="John"):
  print(f"Hello, {name}!")

greet()  # Output: "Hello, John!"
greet("Jane")  # Output: "Hello, Jane!"

def sum_all(*args):
  total = 0
  for number in args:
    total += number
  return total

result = sum_all(1, 2, 3, 4, 5)
print(result)  # Output: 15

def greet(**kwargs):
  for key, value in kwargs.items():
    print(f"{key}: {value}")

greet(name="John", age=30)  # Output: "name: John", "age: 30"

6. Python modules:

import math

x = math.cos(math.pi)
print(x)  # Output: -1.0

from math import pi, cos

x = cos(pi)
print(x)  # Output: -1.0

import math as m

x = m.cos(m.pi)
print(x)  # Output: -1.0

from math import *

x = cos(pi)
print(x)  # Output: -1.0

Exit mobile version