PythonBasics
Basics of Python Programming language
Install / Use
/learn @Nyandwi/PythonBasicsREADME
<a name='0'></a>
Basic of Python Programming Language
This is a practical introduction to Python Programming Language. The style of this repo was inspired by Chip Huyen Cool Python repo. You can find the companion notebook here. For advanced Python concepts, check this repo(work in progress)!
Python is an interpreted, high-level, and general purpose programming language that was designed for efficiency, readability, and simplicity.
Python design philosophy emphasizes simplicity and code readability. There are about 19 Python design guiding principles, the top 8 being:
- Beautiful is better than ugly.
- Explicit is better than implicit
- Simple is better than complex
- Complex is better than complicated
- Readability counts
- Now is better than ever
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
More design rules can be found in the Zen of Python. You can also display them by importing this(import this) in any Python interpreter.
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Python is a popular and go-to programming language in different tech communities, most notable in machine learning and data science. It is sometimes referred to as “batteries included” due to its rich standard library. Below are more elaborated advantages of Python:
- It is simple to read and write: Python syntaxes are very easy to write and easy to recall as well.
- It has a beautiful design and built-in data types.
- It has thousands of great libraries in many disciplines.
- Supportive communities: Good documentation, courses, tutorials, social groups.
- Easy to learn and use due to its simple syntaxes which feel like a natural language.
This introduction will cover the following:
<a name='1'></a>
1. Variables, Numbers, and Strings
<a name='1-1'></a>
1.1 Variables
Below are quick notes about Python variables and other most important things to know before writing actual Python code:
A Variableis a location in memory where we store the data.A variablein Python can either be of 3 data types:integer,float, or astring. Data type specifies the category of the variables.- We can use
type(variable_name)to find the type of givenvariable_name. - In Python, we use
#to addcomments. Comments do not change anything and are not compiled. - If your comment is longer than one line, you can use triple quotes. The lines inside triple quotes are ignore during runtime.
"""
In Python, there is no official way to write long comments, but you can use triple quotes.
The sentence between triple quote are ignored at runtime. You can also use single quote('''....'''). Python treats single quote as double quotes in many scanerios such as strings representation.
Guido also agreed it works: https://twitter.com/gvanrossum/status/112670605505077248
"""
- We also use
=to assign a value to the name of variable. Example:var_name = 1. Note that it's different to comparison operator of equal to (==). - We can use
print()to display the value of variable or the results of any expression. - Each line of the code start on the new line.
- Be aware of indentations. Python is serious about them.
# EXAMPLE OF CREATING A VARIABLE
# We use # to add comment, it won’t run or affect the code
# You use = to assign a value to the name of the variable.
# Each code starts on the new line. No semicolon or {}
# Python is awesome. You do not need to provide the data type of variable when creating it.
int_var = 1
str_var = 'Hundred'
<a name='1-2'></a>
1.2 Numbers
Numbers in Python can either be integers int or floats float. Integer are real, finite, natural or whole numbers. Take an example: 1,2,3,4 are integers. Floats are numbers that have decimal points such as4.6, 6.0, 7.7. Note that 4.0 is considered as a float data type too. Recently, Karpathy, AI Director at Tesla posted that floats are not real.
We can perform operations on numbers. The operations that we can perform include addition, multiplication, division, modular, etc...
int_var = 10
float_var = 12.8
type(int_var)
int
type(float_var)
float
# Numeric Operations
# Addition
1 + 100
101
# Multiplication
1 * 100
100
# Division
1 / 100
0.01
# Floor division
7 // 2
3
# Modular (%)
# This is the remainder or a value remaining after dividing two numbers
# 100 / 1 = 100, remainder is 0
100 % 1
0
1 % 100
1
10 % 2
0
# Powers
# 1 power any number is 1 always
1 ** 100
1
2 ** 2
4
# We use print() to display the results of the operations or a variable
print(1 + 100)
101
<a name='1-3'></a>
1.3 Strings
Python supports strings. String is a sequence of characters.
Strings are one of the commonly used and important data types. Most problems involve working with strings. Thus, knowing how to work with strings is an incredible thing.
Strings are expressed in either "..." or '...'.
"text inside here will be a string"
'text inside here will also be a string'
We can manipulate strings in many ways. A simple example is to concat the strings.
str_var = 'One'
str_var2 = 'Hundred'
str_var + str_var2
'OneHundred'
str_var + ' ' + 'and' + ' '+ str_var2 + '.'
'One and Hundred.'
# We can use print() to display a string
print(" This is a string")
This is a string
# We can also compare strings to check whether they are similar.
# If they are similar, case by case, comparison operator returns true. Else false
"A string" == "a string"
False
"A string" == "A string"
True
Strings Methods
Python provides many built-in methods for manipulating strings. As a programmer, knowing typical string methods and how to use them will give you a real leverage when working with strings.
There are many string methods. You can find them here. Let's see some common methods.
sentence = 'This IS A String'
# Case capitalization
# It return the string with first letter capitalized and the rest being lower cases.
sentence.capitalize()
'This is a string'
# Given a string, convert it into title (each word is capitalized)
sentence_2 = 'this is a string to be titled'
sentence_2.title()
'This Is A String To Be Titled'
# Converting the string to upper case
sentence.upper()
'THIS IS A STRING'
# Converting the string to upper case
sentence.lower()
'this is a string'
# Splitting the string
sentence.split()
['This', 'IS', 'A', 'String']
Lastly, we can use replace() method to replace some characters in string with another characters. Replace method takes two inputs: characters to be replaced, and new characters to be inserted in string, replace('characters to be replaced', 'new characters').
Example, given the string "This movie was awesome", replace the world movie with project.
stri = "This movie was awesome"
stri.replace('movie', 'project')
'This project was awesome'
# In the following string, replace all spaces with `%20'
stri_2 = "The future is great"
stri_2.replace(' ', '%20')
'The%20future%20is%20great'
As you can see, strings methods are powerful and can save you time. Remember one of the Python philosophies that we saw in the beginning: Simple is better than complex.
<a name='2'></a>
2. Data Structures
Data structures are used to organize and store the data. Alg
Related Skills
claude-opus-4-5-migration
96.8kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
model-usage
344.1kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
TrendRadar
50.4k⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
mcp-for-beginners
15.7kThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.
